2. A string is a sequence of character that is treated
as a single data item.
String is represented as a CHARACTER ARRAY.
Declaration of string
Syntax:- char string_name[length];
Example:- char name[5];
This example declares an array name
which can store at the most 5
characters.
3. Character array can be initialiized in two ways- as
individual character or as single string.
Example:-
Char greet[10]= {‘h’, ’e, ’l’, ‘l’, ‘o’, ‘!’};
Char greet[10]=“hello!”;
Char greet[]=“hello!”;
The compiler automatically stores the null
character at the end of the string. Memory
representation of char greet[]=“hello!”; is as
follows:
h e l l o ! 0
4. We are familiar with two standard function for
reading input and writing output – scanf() and
printf() respectively.
These functions with format specifiers %c and
%s can be used for input and output of
characters and strings.
The example given in the next slide.
5. Ex:- char color {6};
Scanf(“%c”, & color); // reading char
Scanf(“%s”, & color); // reading string
printf(“%c”, & color); // writing char
printf(“%s”, & color); // writing string
WE have seen other I/o functions for
character and string –
getchar,putchar,gets,puts in the chapter 2.
6. Strlen(s1):- Return the length of the string s1
excluding the null character.
Strlwr(s1):- Convert the string s1 to lower
case.
Strcat(s1,s2):- Appends a copy of string s2 to
end of s1 and terminates s1 with a null and a
return s1.
Strcmp(s1,s2):- Compress s1 and s2 and
returns –ve if s1<s2 , +ve if s1>s2 , 0 if
s1=s2.
7. Strcpy(s1,s2):- copies the string s2 into
s1,modifying the string s1.
Strcmpi(s1,s2):- Compress s1 and s2
ignoring the case and return similar result as
strcmp.
Strncmp(s1,s2,n):- compress the first n
character of string s1 and s2 and return
similar result as strcmp.
Strupr(s1):- Convert the string s1 to
uppercase.
8. Strchr(s1,c):- Returns a pointer to the first
occurrence of the character c in string s1.
Strrchr(s1,c):- Returns a pointer to the last
occurrence of the character c in string s1.
Strstr(s1,s2):- Returns a pointer to the first
occurrence of the string s2 in string s1.
Strrev(s1):- Returns the reverse string of
string s1.
9. Strtok(s1,s2):- Searches s1 for tokens that
are separated by delimiters specified in s2.
Returns the pointer to first char to first token
in s1.
Steset(s1,c):- Sets all characters in string s1
to character c and quites when null character
is detected.
Strnset(s1,c,n):- Sets the first char in string
s1 to char c and quits when nill char is
detected.
Sscanf:- Divided lines into tokens.