2. Strings
A special kind of array is an array of characters
ending in the null character 0 called string
arrays
A string is declared as an array of characters
char s[10]
char p[30]
When declaring a string don’t forget to leave a
space for the null character which is also known
as the string terminator character
From the desk of Dhanraj from SRICT
3. String storage
•These functions with formate specifiers %c &%s can be used for input & output
of charactors and strings.
Examples :-
Scanf(“%c”,&colour); // reading character
Scanf(“%s”,colour); // reading string
Scanf(“%c”,colour); // writing character
Scanf(“%c”,colour); // writing string
From the desk of Dhanraj from SRICT
4. Strlen(s1) :- returns the length of the string s1 excluding the
null character.
Strlwr(s1):- converts the string s1 to lower case.
Strcat(s1,s2):- appends a copy of string s2 to the end of s1
and terminates s1 with a null and returns s1.
Strcpy(s1,s2):- copies the srring s2 into string s1 ,modifying
the string s1.
Strcmpi(s1,s2):- compares s1 &s2 ignoring the cas and
returns similler result as strcmp.
Strupr(s1):-converts the string s1 to uppercase.
Strchr(s1,c):-returns a pointer to the first occurrence of the
character c in string s1.
Strstr(s1,s2):- returns a pointer to the first occurrence of the
string s1 in string s2.
Strrev(s1):- returns the reverse string of string s1.From the desk of Dhanraj from SRICT
5. Four main operations on strings
strcpy - copy one string into another
strcat - append one string onto the right side of the
other
strcmp – compare alphabetic order of two strings
strlen – return the length of a string
From the desk of Dhanraj from SRICT
7. strcat
strcat(destinationstring, sourcestring)
appends sourcestring to right hand side of
destinationstring
For example if str had value “a big ”
strcat(str, “hello world”); appends “hello world” to
the string “a big ” to get
“ a big hello world”
From the desk of Dhanraj from SRICT
8. strcmp
strcmp(stringa, stringb)
Compares stringa and stringb alphabetically
Returns a negative value if stringa precedes
stringb alphabetically
Returns a positive value if stringb precedes
stringa alphabetically
Returns 0 if they are equal
Note lowercase characters are greater than
Uppercase
From the desk of Dhanraj from SRICT
9. strlen
strlen(str) returns length of string excluding null
character
strlen(“tttt”) = 4 not 5 since 0 not counted
From the desk of Dhanraj from SRICT
10. Input output functions of characters and
strings
getchar() reads a character from the screen in a non-
interactive environment
getche() like getchar() except interactive
putchar(int ch) outputs a character to screen
gets(str) gets a string from the keyboard
puts(str) outputs string to screen
From the desk of Dhanraj from SRICT