1 / 30

Strings

Strings. 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 as string_2 ? Append string_1 at the end of string_2 Sort a collection of strings in ascending order

parry
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings

  2. 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 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

  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 deststr • strcat(dest_str, src_str); //append source str at the end of deststr • 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) for(i=0;i<10;++i) gets(str[i]); After the for loop finishes execution the array of names will look like this: names =

  15. Algorithm – Sorting an array of names (strings) 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) • start from row # 1 (variable i) • Compare row 1 with rows 2, 3 and 4 (up to i-1), one row at a time • Smaller of the two names being compared must occupy the upper row Col 1, col 2, col 3, …. Col 50 Row 1 Row 2 Row 3 Row 4

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

  18. Algorithm – Sorting an array of names (strings) The array now looks like this i 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 … j

  19. Algorithm – Sorting an array of names (strings) Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  20. Algorithm – Sorting an array of names (strings) Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  21. Algorithm – Sorting an array of names (strings) i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  22. Algorithm – Sorting an array of names (strings) Array now looks like this i 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 … j

  23. Algorithm – Sorting an array of names (strings) Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  24. Algorithm – Sorting an array of names (strings) Array now looks like this i 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 … j

  25. Algorithm – Sorting an array of names (strings) i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  26. Algorithm – Sorting an array of names (strings) Compare and swap if(strcmp(str[i],str[j])> 0) i 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 … j

  27. Algorithm – Sorting an array of names (strings) Finally, the sorted array looks like this 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 …

  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

More Related