03 Programming 1 Lecture Strings in C v01
03 Programming 1 Lecture Strings in C v01
Lecture 3b
An Introduction to String Manipulation in
the C language
1
Facilitators
SCIT’s Phone Number: 876-9271680-8 ext 2164
• It is represented by “\0”.
• important for strlen() – length doesn’t include the NULL (will explain later)
8
Declaration Of A String
•Do not use String as data type because String data type is included in later languages such as C+
+ / Java. C does not support String data type
•When you are using string for other purpose than accepting and printing data then you must
include following header file in your code– #include<string.h>
12
Initializing String (Character Array)
• The process of assigning some legal default data to String is called
initialization of string.
• A string can be initialized in different ways. We will explain this with the help
of an example.
• Below is an example to declare a string with name as str and initialize it with
“GeeksforGeeks”.
o char str[] = "GeeksforGeeks";
o char str[50] = "GeeksforGeeks";
o char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
o char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'}; 13
String Initializing Example – Activity 3b.2
• If destination string length is less than source string, entire source string value won’t be copied
into destination string. For example, consider destination string length is 20 and source string
length is 30. Then, only 20 characters from source string will be copied into destination
string and remaining 10 characters won’t be copied and will be truncated .
String Copy - Activity 3b.3
Copy the following code in your C programming IDE
#include <stdio.h>
#include <string.h>
int main()
{
char string1[50] = “I like programming”;
char string2[50];
strcpy(string2, string1); //copies value on the right to variable on the left
printf(“%s”, string2); //outputs: I like programming
return 0;
}
Activity 3b.3
27
String Length (strlen) - Activity 3b.6
Copy the following code in your C programming IDE:
#include <stdio.h>
#include <string.h>
int main()
{
char word[50];
int wordLength;
printf("Enter a word without spaces: ");
scanf("%s", word);
wordLength = strlen(word);
printf("The %s has %d characters", word, wordLength);
return 0;
}
Activity 3b.6
https://www.gangainstitute.com/wp-content/uploads/2020/04/C
P-STRING.ppt
ESCAPE SEQUENCES
Escape Sequences In C
• As the name denotes, the escape sequence denotes the scenario
in which a character undergoes a change from its normal form
and denotes something that is different than its usual meaning.
• Generally, an escape sequence begins with a backslash ‘\’
followed by a character or characters. The c compiler interprets
any character followed by a ‘\’ as an escape sequence. Escape
sequences are used to format the output text and are not
generally displayed on the screen. Each escape sequence has its
own predefined function.
• Source: https://www.educba.com/escape-sequence-in-c/
Escape Sequences in C
• \n (New line) – We use it to shift the cursor control to the
new line
• \t (Horizontal tab) – We use it to shift the cursor to a
couple of spaces to the right in the same line.
• \a (Audible bell) – A beep is generated indicating the
execution of the program to alert the user.
• \r (Carriage Return) – We use it to position the cursor to
the beginning of the current line.
• \\ (Backslash) – We use it to display the backslash
character.
Escape Sequences in C
• \’ (Apostrophe or single quotation mark) – We use it to
display the single-quotation mark.
• \” (Double quotation mark)- We use it to display the
double-quotation mark.
• \0 (Null character) – We use it to represent the
termination of the string.
• \? (Question mark) – We use it to display the question
mark. (?)
• \nnn (Octal number)- We use it to represent an octal
number.
• \xhh (Hexadecimal number) – We use it to represent a
hexadecimal number.
• \v (Vertical tab)
FORMAT SPECIFIERS
Format Specifiers
• In C programming language, values can be type integer,
floating-point, single character, or sequence of characters.
We use format specifiers in C to display values of a
variable of a different type.
• C contains different format specifiers used in printf() and
scanf() functions; in this tutorial, we will go through a few
important and most commonly used format specifiers in
our C programs.
Why Do We Use C Format
Specifiers?
• Format specifiers in C are used to take inputs and print
the output of a type. The symbol we use in every format
specifier is %. Format specifiers tell the compiler about
the type of data that must be given or input and the type
of data that must be printed on the screen.
The Most Commonly Used Format
Specifiers in C
Format Specifiers Type of Output
%c Signed character
%f Signed float
%e A floating-point number
%lf double
The Most Commonly Used Format
Specifiers in C
Format Specifiers Type of Output
%o Octal integer
42