1 / 61

Chapter 10 - Character Strings

Chapter 10 - Character Strings. Array of Characters. char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ };. Program 10.1. /* * Function to concatenate two character strings */ #include <stdio.h> concat (char result[], char str1[], int n1, char str2[], int n2) { int i;

callie-rice
Download Presentation

Chapter 10 - Character 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. Chapter 10 - Character Strings

  2. Array of Characters char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ };

  3. Program 10.1 /* * Function to concatenate two character strings */ #include <stdio.h> concat (char result[], char str1[], int n1, char str2[], int n2) { int i; // copy str1 to result for (i = 0; i < n1; i++) result[i] = str1[i]; for (i = 0; i < n2; i++) result[n1 + i] = str2[i]; }

  4. Program 10.1 (continued) main() { char s1[5] = {'T', 'e', 's', 't', ' '}; char s2[6] = {'w', 'o', 'r', 'k', 's', '.'}; char s3[11]; int i; concat (s3, s1, 5, s2, 6); for (i = 0; i < 11; i++) printf("%c", s3[i]); printf ("\n"); system ("PAUSE"); }

  5. Program 10.1 Output

  6. Variable Length Character Strings char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’ }; NULL Character Terminates a String

  7. Program 10.2 /* * Function to count the number of characters in a string */ #include <stdio.h> int string_length (char string[]) { int count = 0; while(string[count] != '\0') count++; return (count); }

  8. Program 10.2 (continued) main() { char word1[] = {'a', 's', 't', 'e', 'r', '\0'}; char word2[] = {'a', 't', '\0'}; char word3[] = {'a', 'w', 'e', '\0'}; int i; printf ("%i %i %i\n", string_length (word1), string_length (word2), string_length (word3)); system ("PAUSE"); }

  9. Program 10.2 Output

  10. Initializing and Displaying Character Strings These Statements Are Equivalent char word[] = “Hello!”; char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’ }; char word[7] = “Hello!”; char word[6] = “Hello!”; Leave Room for the NULL Character

  11. Program 10.3 /* * Function to concatenate two character strings */ #include <stdio.h> concat (char result[], char str1[], char str2[]) { int i; int j; // copy str1 to result for (i = 0; str1[i] != '\0'; i++) result[i] = str1[i]; for (j = 0; str2[j] != '\0'; j++) result[i + j] = str2[j]; result[i+j] = '\0'; }

  12. Program 10.3 (continued) main() { char s1[] = "Test "; char s2[] = "works."; char s3[20]; concat (s3, s1, s2); printf("%s\n", s3); system ("PAUSE"); }

  13. Program 10.3 Output

  14. Testing Two Character Strings for Equality Since the C Programming Language does not support a data type of string we cannot directly test two strings to see if they are equal with a statement such as if ( string1 == string2 )

  15. <string.h>

  16. Program 10.4 /* * Function to determine if two strings are equal */ #include <stdio.h> #define TRUE 1 #define FALSE 0 int equal_strings (char s1[], char s2[]) { int i = 0; while ((s1[i] == s2[i]) && (s1[i] != '\0') && (s2[i] != '\0')) i++; if ((s1[i] == '\0') && (s2[i] == '\0')) return(TRUE); else return(FALSE); }

  17. Program 10.4 (continued) main() { char stra[] = "string compare test"; char strb[] = "string"; printf ("%i\n", equal_strings (stra, strb)); printf ("%i\n", equal_strings (stra, stra)); printf ("%i\n", equal_strings (strb, "string")); system ("PAUSE"); }

  18. Program 10.4 Output

  19. Inputting Character Strings char string[81]; scanf ( “%s”, string); char s1[81], s2[81], s3[81]; scanf ( “%s%s%s”, s1, s2, s3);

  20. Program 10.5 /* * Program to illustrate the %s scanf format characters */ #include <stdio.h> main() { char s1[81]; char s2[81]; char s3[81]; printf ("Enter text:\n"); scanf ("%s%s%s", s1, s2, s3); printf ("\ns1 = %s\ns2 = %s\ns3 = %s\n", s1, s2, s3); system ("PAUSE"); }

  21. Program 10.5 Output

  22. Program 10.6 /* * Function to read a line of text from a terminal */ #include <stdio.h> read_line (char buffer[]) { char character; int i = 0; do { character = getchar(); buffer[i] = character; i++; } while (character != '\n'); buffer[i-1] = '\0'; }

  23. Program 10.6 (continued) main() { char line[81]; int i; for (i=0; i < 3; i++) { read_line(line); printf ("%s\n\n", line); } system ("PAUSE"); }

  24. Program 10.6 Output

  25. Program 10.7 #include <stdio.h> #define TRUE 1 #define FALSE 0 /* * Function to determine if a character is alphabetic */ alphabetic (char c) { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) return (TRUE); else return (FALSE); }

  26. Program 10.7 (continued) /* * Function to count the number of words in a string */ count_words (char string[]) { int looking_for_word = TRUE; int word_count = 0; int i; for (i=0; string[i] != '\0'; i++) { if (alphabetic(string[i])) { if (looking_for_word) { word_count++; looking_for_word = FALSE; } } else looking_for_word = TRUE; } return (word_count); }

  27. Program 10.7 (continued) main() { char text1[] = "Well, here goes."; char text2[] = "And here we go... again."; printf ("%s - words = %i\n", text1, count_words (text1)); printf ("%s - words = %i\n", text2, count_words (text2)); system ("PAUSE"); }

  28. Program 10.7 Output

  29. Execution of count_words Function

  30. The NULL String A Character String that contains no characters other than the NULL Character has a special name in the C Programming Language, it is called the NULL String. The string length will correctly return 0. char buffer[100] = “”;

  31. Program 10.8 #include <stdio.h> #define TRUE 1 #define FALSE 0 /***** Insert alphabetic function here *****/ /***** Insert read_line function here *****/ /***** Insert count_words function here *****/

  32. Program 10.8 (continued) main() { char text[81]; int end_of_text = 0; int total_words = 0; printf ("Type in your text.\n"); printf ("When you are done, press 'RETURN'.\n\n"); while (!end_of_text) { read_line (text); if (text[0] == '\0') end_of_text = TRUE; else total_words += count_words (text); } printf("\nThere are %i words in the above text.\n", total_words); system ("PAUSE"); }

  33. Program 10.8 Output

  34. Escape Characters

  35. More on Constant Strings If you put a backslash character at the very end of the line and followed it immediately by a carriage return, it will tell the C Compiler to ignore the end of line. This line continuation technique is used primarily for continuing long constant character strings. char letters[] = “abcdefghijklmnopqrstuvwxyz\ ABCDEFGHIJKLMNOPQRSTUVWXYZ”; An even easier way of breaking up long character strings is to divide them into two or more adjacent strings. char letters[] = “abcdefghijklmnopqrstuvwxyz” “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

  36. Character Strings, Structures, and Arrays Suppose we wanted to write a computer program that acted as a dictionary. One of the first thoughts would be to representation of the word and it definition. Since the word and its definition are logically related, the notion of a structure comes immediately to mind. struct entry { char word[10]; char definition[50]; }; struct entry dictionary[100];

  37. Program 10.9 /* * Dictionary lookup program */ #include <stdio.h> #define TRUE 1 #define FALSE 0 struct entry { char word[10]; char definition[50]; }; /***** Insert equal_string function here *****/

  38. Program 10.9 /* * Function to lookup a word inside a dictionary */ int lookup (struct entry dictionary[], char search [], int entries) { int i; for (i=0; i < entries; i++) if (equal_strings (search, dictionary[i].word)) return (i); return (-1); }

  39. Program 10.9 main() { struct entry dictionary[100] = { { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; char word[10]; int entries = 10; int entry_number;

  40. Program 10.9 printf("Enter word: "); scanf("%9s", word); entry_number = lookup (dictionary, word, entries); if (entry_number != -1) printf ("%s\n", dictionary[entry_number].definition); else printf ("Sorry, that word is not in my dictionary.\n"); system ("PAUSE"); }

  41. Program 10.9 Output

  42. A Better Search Method Binary Search Algorithm Step 1: Set low to 0, high to n – 1 Step2: If low > high, x does not exist in M and the algorithm terminates Step 3: Set mid to (low + high) / 2 Step 4: if M[mid] < x, set low to mid +1 and go to Step 2 Step 5: if M[mid] > x, set high to mid -1 and go to Step 2 Step 6: M[mid] equals x and the algorithm terminates

  43. Binary Search Algorithm /* * Binary Search Algorithm */ int lookup (struct entry M[], char x[], int n) { int low = 0; /* Step 1: */ int high = n - 1; /* Step 1: */ int mid; int result; while (low <= high) { mid = (low + high) / 2; /* Step 3: */ result = compare_strings (M[mid].word, x); if (result == -1) low = mid + 1; /* Step 4: */ else if (result == 1) high = mid - 1; /* Step 5: */ else return (mid); /* Step 6: */ } return (-1); /* Step 2: */ }

  44. Examples of Binary Search

  45. Examples of Binary Search

  46. Examples of Binary Search

  47. Program 10.10 /* * Dictionary lookup program */ #include <stdio.h> struct entry { char word[10]; char definition[50]; };

  48. Program 10.10 (continued) /* * Function to compare two character strings */ int compare_strings (char s1[], char s2[]) { int i = 0; while ((s1[i] == s2[i]) && (s1[i] != '\0') && (s2[i] != '\0')) i++; if (s1[i] < s2[i] ) /* s1 < s2 */ return(-1); else if (s1[i] == s2[i]) /* s1 == s2 */ return (0); else /* s1 > s2 */ return(+1); }

  49. Program 10.10 (continued) /* * Function to lookup a word inside a dictionary */ int lookup (struct entry dictionary[], char search [], int entries) { int low = 0; int high = entries - 1; int mid; int result; while (low <= high) { mid = (low + high) / 2; result = compare_strings (dictionary[mid].word, search); if (result == -1) low = mid + 1; else if (result == 1) high = mid - 1; else return (mid); /* found it */ } return (-1); /* not found */ }

  50. Program 10.10 (continued) main() { struct entry dictionary[100] = { { "aardvark", "a burrowing African mammal" }, { "abyss", "a bottomless pit" }, { "acumen", "mentally sharp; keen" }, { "addle", "to become confused" }, { "aerie", "a high nest" }, { "affix", "to append; attach" }, { "agar", "a jelly made from seaweed" }, { "ahoy", "a nautical call of greeting" }, { "aigrette", "an ornamental cluster of feathers" }, { "ajar", "partially opened" } }; char word[10]; int entries = 10; int entry_number;

More Related