Date post: | 30-Dec-2015 |
Category: |
Documents |
Upload: | isabel-york |
View: | 249 times |
Download: | 2 times |
STRINGSSTRINGS&&
STRING HANDLING FUNCTIONS STRING HANDLING FUNCTIONS
The char Data TypeThe 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 integerFor example the ASCII code associates the character ‘m’ with the integer 109.
Internally, C treats character variables as integers.
ASCII CHARACTER SETASCII CHARACTER SET
WHAT ARE STRINGSWHAT ARE STRINGSIn C strings are represented as array of
charactersStrings 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 stringASCII value of \0 is 0
Representation of strings in memoryRepresentation of strings in memory
The fig shows how the string “college” is represented in the computers memory
Declaring strings Declaring strings
Reading string inputsReading 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 inputsReading 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 inputsReading string inputs
The gets( ) is an inbuilt function available with <stdio.h> library. This function can read multiple words which are separated by spaces
Printing/displaying the stringsPrinting/displaying the strings
PROBLEM 1: Write a PROBLEM 1: Write a program to store the name program to store the name of a student and then of a student and then display the same display the same
PROBLEM 2: Write a program PROBLEM 2: Write a program to find the length of the to find the length of the name(without using string name(without using string function)function)
PROBLEM 3: Write a program PROBLEM 3: Write a program to check whether two strings to check whether two strings are equalare equal(without using string (without using string functions) functions)
PROBLEM 4: Write a program PROBLEM 4: Write a program to count the number of vowels to count the number of vowels in a string in a string
PROBLEM 4: Write a program PROBLEM 4: Write a program to count the number of to count the number of words in a string words in a string
Lot of string handling functions are available in the header file <string.h>
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.HFUNCTIONS IN STRING.H
SOME POPULAR ONESSOME POPULAR ONES
Examples of String Examples of String FunctionsFunctions
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 Write a program to compare two strings and compare two strings and to concatenate them if to concatenate them if they are unequalthey are unequal
PROBLEM 5: Write a PROBLEM 5: Write a program to check whether a program to check whether a given string is palindrome given string is palindrome or notor not
PROBLEM 2: Write a program PROBLEM 2: Write a program to sort a list of names in to sort a list of names in ascending order ascending order