Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.

Similar presentations


Presentation on theme: "Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same."— Presentation transcript:

1 Strings

2 Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same as string_2 ? – Append string_1 at the end of string_2 – Sort a collection of strings in ascending order – Find occurrence of a character in a given sentence (string)

3 Strings The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0‘. The following declaration and initialization create a string consisting of the word "Hello“: char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

4 Strings This is how this char array looks in memory contains the above string Be careful about the length of string’s array length No bounds checking by the compiler May result in unpredictable results Hello\0

5 Strings Example – Placement of NULL Write a program to declare a char array, initialize it to the string “Hello” and then move around the NULL character to see its effects

6 Strings Manipulation Because dealing with strings is cumbersome, a library of functions is available for programmers to manipulate strings Some of the more frequently used string manipulation functions are: – strlen(str); //returns length of a str – strcpy(dest_str, src_str);//copies source str in dest str – strcat(dest_str, src_str); //append source str at the end of dest str – strcmp(Str1, Str2);//compares two strings lexicographically //returns 0 if both are equal, negative number if LHS is < RHS

7 Strings Example – strlen() function Write a program to declare a char array, initialize it to a string and then traverse the string in forward and backward direction

8 Strings Example – strcpy(), strcat() functions Write a program to declare two char arrays and initialize them to some strings. Declare a third char array and copy a concatenation of the first two strings into the third array.

9 String I/O Another convenient way for string I/O is: – gets(str); //read a string from user and store in str – puts(str); //print a string contained in str

10 Strings Example – strcmp() function Write a program to declare two char arrays and initialize them to some strings. Compare the two strings using strcmp() function

11 Strings Example – strcmp() function Write a program to input a list of names. Sort the list of names in ascending (lexicographical / dictionary) and descending orders.

12 Algorithm – Sorting an array of names (strings) You have a 2-Dimensional array containing 4 names char names[4][50]; The array has 4 rows and 50 columns each row can hold one name of length 50 characters (including the NULL that comes at the end of a string

13 Algorithm – Sorting an array of names (strings) To input names, you only need to specify the row where the name is to be stored for(i=0;i<10;++i) gets(str[i]); Here, i is the index of the row where every name will be stored The function gets() takes care of where every character of a name goes and also the string terminating NULL character

14 Algorithm – Sorting an array of names (strings)

15 Loops to compare strings and swap places for(i=0;i<3;++i) for(j=i+1;j<4 ;++j){ if(strcmp(str[i],str[j])> 0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); }

16 Algorithm – Sorting an array of names (strings) Row 1 Row 2 Row 3 Row 4 Col 1, col 2, col 3, …. Col 50

17 Algorithm – Sorting an array of names (strings) i j Compare and swap if(strcmp(str[i],str[j])> 0)

18 Algorithm – Sorting an array of names (strings) The array now looks like this a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j

19 Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

20 Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

21 Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0)

22 Algorithm – Sorting an array of names (strings) a d a m \0 … s o p h i a \0 … t y l e r \0 … r a c h e l \0 … i j Array now looks like this

23 Algorithm – Sorting an array of names (strings) a d a m \0 … s o p h i a \0 … t y l e r \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

24 Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … t y l e r \0 … s o p h i a \0 … i j Array now looks like this

25 Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … t y l e r \0 … s o p h i a \0 … i j i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0)

26 Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … s o p h i a \0 … t y l e r \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

27 Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … s o p h i a \0 … t y l e r \0 … Finally, the sorted array looks like this

28 Strings Example – counting words Write a program to input a sentence (string) and count the number of words in it.

29 Strings Example – frequency of a character Write a program to input a sentence (string) and find the frequency of a given character.

30 Strings Example – Palindrome or Not Write a program to input a word and find out if it is a palindrome. Civic, mom, dad, kayak, radar, madam, racecar Function strrev(str) reverses a string


Download ppt "Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same."

Similar presentations


Ads by Google