Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area

Similar presentations


Presentation on theme: "Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area"— Presentation transcript:

1 Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Hrs 4 Strings 2 Character Arrays and Strings, String Manipulation KIIT UNIVERSITY

2 What are Strings KIIT UNIVERSITY
Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character i.e. ‘\0’, which is a character all of whose bits are zero. A string constant is a one-dimensional array of characters. For example, char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ; Each character in the array occupies one byte of memory and the last character is always ‘\0’. What character is this? It looks like two characters, but it is actually only one character, with the \indicating that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48. KIIT UNIVERSITY

3 Printing of a string KIIT UNIVERSITY Code 1 Code 2 Code 3
The terminating null (‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a collection of characters. C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings. For example, the string used above can also be initialized as, char name[ ] = "HAESLER" ; Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically. Code 1 Code 2 Code 3 char name[ ] = "Klinsman" ; int i = 0 ; while ( i <= 7 ) { printf ( "%c", name[i] ) ; i++ ; } char name[ ] = "Klinsman" ; int i = 0 ; while ( name[i] != `\0' ) { printf ( "%c", name[i] ) ; i++ ; } char name[ ] = "Klinsman" ; char *ptr ; ptr = name ; /* store base address of string */ while ( *ptr != `\0' ) { printf ( "%c", *ptr ) ; ptr++ ; } KIIT UNIVERSITY

4 Printing of a string KIIT UNIVERSITY
There are so many ways (as shown in previous slide) to refer to the elements of a character array, rarely is any one of them used. This is because printf( ) function has got a sweet and simple way of doing it, as shown below. Note that printf( ) doesn’t print the ‘\0’. int main( ) { char name[ ] = "Klinsman" ; printf ( "%s", name ) ; return 0; } The %s used in printf( ) is a format specification for printing out a string. The same specification can be used to receive a string from the keyboard, as shown below. char name[25] ; printf ( "Enter your name " ) ; scanf ( "%s", name ) ; printf ( "Hello %s!", name ) ; KIIT UNIVERSITY

5 Entering the string using scanf( )
While entering the string using scanf( ) we must be cautious about two things: The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event, you would have nobody to blame but yourselves. scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Hrithik Roshan’ would be unacceptable. The way to get around this limitation is by using the function gets( ). The usage of functions gets( ) and its counterpart puts( ) is shown below. char name[25] ; printf ( "Enter your full name " ) ; gets ( name ) ; puts ( name ) ; KIIT UNIVERSITY

6 Standard Library String Function
KIIT UNIVERSITY

7 Standard Library String Function
KIIT UNIVERSITY

8 strlen() KIIT UNIVERSITY Output
Declaration - size_t strlen(const char *str) where size_t is the unsigned integral type and str is the base address of the string Note - Computes the length of the string up to but not including the terminating null character. int main( ) { char arr[ ] = "Bamboozled" ; int len1, len2 ; len1 = strlen ( arr ) ; len2 = strlen ( "Humpty Dumpty" ) ; printf ( "\nstring = %s length = %d", arr, len1 ) ; printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ; return 0; } Output string = Bamboozled length = 10 string = Humpty Dumpty length = 13 KIIT UNIVERSITY

9 strcpy() KIIT UNIVERSITY Output
Declaration - char *strcpy(char *dest, const char *src) where dest is the pointer to the destination array where the content is to be copied and src is the string to be copied and the function returns a pointer to the destination string dest. int main( ) { char source[ ] = "Sayonara" ; char target[20] ; strcpy ( target, source ) ; printf ( "\nsource string = %s", source ) ; printf ( "\ntarget string = %s", target ) ; return 0; } Output source string = Sayonara target string = Sayonara KIIT UNIVERSITY

10 strcat() KIIT UNIVERSITY Output
Declaration - char *strcat(char *dest, const char *src) where dest is the pointer to the destination array where the content is to be copied and should be large enough to contain the concatenated resulting string. src is the string to be copied and the function returns a pointer to the destination string dest. int main( ) { char source[ ] = "Students!" ; char target[30] = "Hello" ; strcat ( target, source ) ; printf ( "\nsource string = %s", source ) ; printf ( "\ntarget string = %s", target ) ; return 0; } Output source string = Students! target string = HelloStudents! KIIT UNIVERSITY

11 strcmp() KIIT UNIVERSITY Output
Declaration - int strcmp(const char *str1, const char *str2). str1 is the first string to be compared and str2 is the second string to be compared. Return Value - This function return values that are as follows: if return value < 0 then it indicates str1 is less than str2. if return value > 0 then it indicates str2 is less than str1. if return value = 0 then it indicates str1 is equal to str2. int main( ) { char string1[ ] = "Jerry" ; char string2[ ] = "Ferry" ; int i, j, k ; i = strcmp ( string1, "Jerry" ) ; j = strcmp ( string1, string2 ) ; k = strcmp ( string1, "Jerry boy" ) ; printf ( "\n%d,%d,%d", i, j, k ) ; return 0; } Output 0,4,-32 KIIT UNIVERSITY

12 Two-Dimensional Array of Characters
#define FOUND 1 #define NOTFOUND 0 int main( ) { char masterlist[6][10] = { "akshay", "parag", "raman", "srinivas", "gopal", "rajesh" } ; int i, flag, a ; char yourname[10] ; printf ( "\nEnter your name " ) ; scanf ( "%s", yourname ) ; flag = NOTFOUND ; for ( i = 0 ; i <= 5 ; i++ ) a = strcmp ( &masterlist[i][0], yourname ) ; if ( a == 0 ) printf ( "Welcome, you can enter the palace" ) ; flag = FOUND ; break ; } if ( flag == NOTFOUND ) printf ( "Sorry, you are a trespasser" ) ; return 0; Memory Map KIIT UNIVERSITY

13 Passing Strings to Functions
String can be passed to function in similar manner as arrays as, string is also an array. #include <stdio.h> #include <string.h> void display(char ch[]); int main() { char c[50]; printf("Enter string: "); gets(c); display(c); // Passing string to function. return 0; } void display(char ch[]) printf("String Output: "); puts(ch); KIIT UNIVERSITY

14 Class Work (CW) KIIT UNIVERSITY
WAP to convert the string from lower case to upper case WAP to convert the string from upper case to lower case WAP to reverse the string KIIT UNIVERSITY

15 Thank You KIIT UNIVERSITY

16 Home Work (HW) KIIT UNIVERSITY
WAP that extracts part of the given string from the specified position. For example, if the sting is "Working with strings is fun", then if from position 4, 4 characters are to be extracted then the program should return string as "king". Moreover, if the position from where the string is to be extracted is given and the number of characters to be extracted is 0 then the program should extract entire string from the specified position. WAP that replaces two or more consecutive blanks in a string by a single blank. For example, if the input is Grim return to the planet of apes!! the output should be Grim return to the planet of apes!! WAP to sort a set of names stored in an array in alphabetical order. WAP to delete all vowels from a sentence. Assume that the sentence is not more than 80 characters long. WAP to check whether the given string is palindrome. A palindrome is a word or a number or a sequence of words that can be read the same way from either direction, be it forwards or backwards. E.g. EYE, RACECAR or MADAM are palindrome. WAP to remove characters in string except alphabets KIIT UNIVERSITY

17 Home Work (HW) WAP that receives the month and year from the keyboard as integers and prints the calendar in the following Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated. KIIT UNIVERSITY


Download ppt "Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area"

Similar presentations


Ads by Google