1 / 35

C Programming

C Programming. Character Strings Lecture 7. Topics. Strings Representation Declaration Functions Common mistakes Index of a char in a string. On the Nature of Strings. Recall: Main memory contiguous array of cells each cell has an address. 0x1FFE. 0x1FFF. 0x2000. 0x2001.

betty
Download Presentation

C Programming

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. C Programming Character Strings Lecture 7

  2. Topics • Strings • Representation • Declaration • Functions • Common mistakes • Index of a char in a string

  3. On the Nature of Strings • Recall: Main memory • contiguous array of cells • each cell has an address 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc.

  4. ch On the Nature of Strings (cont.) • Recall:Variable declaration • sets aside a “box” to contain a value Example: char ch; ch = ‘B’; Memory address 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc. ‘B’

  5. On the Nature of Strings (cont.) • String declaration • sets aside an array of cells • each cell contains a char • address of first cell in the array Example: char name[5]; Specifies number of cells in the array

  6. name is 0x2000 On the Nature of Strings (cont.) • String declaration • sets aside an array of cells • each cell contains a char • address of first cell in the array Example: char name[5]; 0x2000 0x2004

  7. Character Strings Declaration 1: char name[5]; Declaration 2: #define MAXLENGTH 5 char name[MAXLENGTH]; name is 0x2000 0x2000 0x2004

  8. String Input/Output No ampersand (&)! #include <stdio.h> #define MAXLENGTH 15 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; scanf("%s %s", string1, string2); printf("%s %s\n", string1, string2); return 0; }

  9. More on scanf-Reading strings No ampersand (&) in scanf with strings! int main() { char user[NAMELEN]; printf("Who are you? "); scanf printf("%s\n", user); return 0; } ("%s", user);

  10. Character Strings • Terminating Character: • Marks the end of string • Special char:’\0’ • It’s really 8 zero bits Initialization: char name[5] = “Ann”; name is 0x2000 A n n \0 0x2000 0x2004

  11. Character Strings Can store at most4 letters, because of `\0’ Initialization: char name[5] = “Ann”; name is 0x2000 A n n \0 0x2000 0x2004

  12. A Char in a String The size of a character string is fixed. You can reference a character at position index: string[index] The first character has index 0

  13. A Char in a String 0x3995 0x399C name is 0x3995 J o h n \0 index 0 index 4 char name[8] = “John”; int i = 2; printf(“Char at index %d is %c.\n”, i, name[i]); output:Char at index 2 is h.

  14. A Char in a String 0x3995 0x399C name is 0x3995 J o h n \0 index 2 char name[8] = “John”; name[2] = ‘X’; printf(“Name: %s\n”, name);

  15. A Char in a String 0x3995 0x399C name is 0x3995 J o X n \0 index 2 char name[8] = “John”; name[2] = ‘X’; printf(“Name: %s\n”, name); output:Name: JoXn

  16. String Operations • #include <string.h> • Operations: • Assignment:strcpy() • Concatenation: strcat() • Comparison: strcmp(), stricmp() • Length:strlen() • See text (p38 to 39) (p108 to 109)

  17. String Operation: Assignment #include <stdio.h> #include <string.h> #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1:<garbage> string2:<garbage>

  18. String Operation: Assignment #include <stdio.h> #include <string.h> #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1:“Hello World!” string2:<garbage>

  19. String Operation: Assignment #include <stdio.h> #include <string.h> #define MAXLENGTH 100 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Hello World!”); strcpy(string2, string1); return 0; } string1:“Hello World!” string2:“Hello World!”

  20. Common Mistake: Wrong Assignment Example 1: char name1[5] = “Ann”; char name2[5] = “Dave”; name2 = name1; Error:“LValue required....”

  21. Common Mistake: Bad Assignment name2 = name1; 0x2000 A n n \0 name1 0x2000 0x2003 0x2000 D a v e \0 name2 0x3990 0x3994

  22. Common Mistake: Not enough space #define MAX 4 char name[MAX] = “Ann”; strcpy(name, “David”); name is 0x2000 A n n \0 0x2000 0x2003

  23. Common Mistake: Not enough space #define MAX 4 char name[MAX] = “Ann”; strcpy(name, “David”); name is 0x2000 D a v i d \0 0x2000 0x2003

  24. String Operation: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, string2); strcat(string1, “World!”); string1:“Goodbye” string2:“, Cruel “

  25. String Operation: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, string2); strcat(string1, “World!”); string1:“Goodbye, Cruel ” string2:“, Cruel “

  26. String Operation: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, string2); strcat(string1, “World!”); string1:“Goodbye, Cruel , Cruel ” string2:“, Cruel “

  27. String Operation: Concatenation char string1[MAXLENGTH]; char string2[MAXLENGTH]; strcpy(string1, “Goodbye”); strcpy(string2, “, Cruel ”); strcat(string1, string2); strcat(string1, string2); strcat(string1, “World!”); string1:“Goodbye, Cruel , Cruel World!” string2:“, Cruel “

  28. Common Mistake: Not enough space char name[5]; strcpy(name, “Ann”); strcat(name, “ Smith”); name is 0x2000 A n n \0 0x2000 0x2004

  29. Common Mistake: Not enough space #define MAX 5 char name[MAX]; strcpy(name, “Ann”); strcat(name, “ Smith”); name is 0x2000 A n n S m i t h \0 0x2000 0x2004

  30. String Operation: Comparison strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (strcmp(string1, string2) < 0) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); } output:Apple Wax

  31. String Operation: Comparison strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (strcmp(string1, string2) < 0) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); } Returns: negative if string1 < string2 zero if string1 == string2 positive if string1 > string2

  32. Common Mistake: Wrong Comparison strcpy(string1, “Apple”); strcpy(string2, “Wax”); if (string1 < string2) { printf(“%s %s\n”, string1, string2); } else { printf(“%s %s\n”, string2, string1); }

  33. Number of char’s before the `\0’. String Operation: Length #define MAX 100 char string1[MAX]; int len strcpy(string1, “Apple”); len=strlen(string1); printf(“%d\n”, len); output:5

  34. name is 0x3990 D a v i d \0 0x3990 0x3994 Common Mistake: Not enough space #define MAX 5 char name[MAX]; strcpy(name, “David”); Don’t forget the ‘\0’.

  35. Summary • A string is a contiguous array of chars that ends with the null character ‘\0’ • The string identifier is the address of the first char in the string • Individual chars are accessed using the str[index] notation • There are C library functions for copying, concatenating and comparing strings.

More Related