STRINGS & STRING HANDLING FUNCTIONS STRINGS & STRING HANDLING FUNCTIONS
The char Data Type Variables of type char take up typically one byte in memory and are used to store printable and non-printable characters. The ASCII code is used to associate each character with an integer For example the ASCII code associates the character ‘m’ with the integer 109. Internally, C treats character variables as integers.
ASCII CHARACTER SET
WHAT ARE STRINGS In C strings are represented as array of characters Strings ie character arrays are always terminated by a null character(\0) at the end by default Note \0 is used to identify the end of the string ASCII value of \0 is 0
Representation of strings in memory The fig shows how the string “college” is represented in the computers memory
Declaring strings
Reading string inputs Here the string is read with the help of a loop control character by character. This method is very rarely used The declaration char code[20] ; must be done earlier in the program
Reading string inputs Here a single word is read. In case of multiple words separated by spaces only the first word is read. This method is used when we are sure that the inputs will not contain multiple words separated by spaces Note: While scanning the ‘&’ symbol is not used as in other cases
Reading string inputs The gets( ) is an inbuilt function available with library. This function can read multiple words which are separated by spaces
Printing/displaying the strings
PROBLEM 1: Write a program to store the name of a student and then display the same
PROBLEM 2: Write a program to find the length of the name(without using string function)
PROBLEM 3: Write a program to check whether two strings are equal (without using string functions)
PROBLEM 4: Write a program to count the number of vowels in a string
PROBLEM 4: Write a program to count the number of words in a string
Lot of string handling functions are available in the header file So whenever we want to make use of these inbuilt library functions in our program then we must include the string.h file
FUNCTIONS IN STRING.H
SOME POPULAR ONES
Examples of String Functions Here are some examples of string functions in action: char s1[]="big sky country"; char s2[]="blue moon"; char s3[]="then falls Caesar ";
Write a program to compare two strings and to concatenate them if they are unequal
PROBLEM 5: Write a program to check whether a given string is palindrome or not
PROBLEM 2: Write a program to sort a list of names in ascending order