1 / 49

Introduction to Comput ing Lecture 11 Character Strings

This lecture provides an introduction to character strings in computing, including their representation, declaration, functions, common mistakes, and indexing. Topics covered include string manipulation, memory organization, and input/output operations.

pingram
Download Presentation

Introduction to Comput ing Lecture 11 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. Introduction to ComputingLecture 11Character Strings • Assist.Prof.Dr.NükhetÖZBEK • Ege University • Department of Electrical&Electronics Engineering • nukhet.ozbek@ege.edu.tr

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

  3. Introduction • Grouping of characters is called a string • As C implements the strings using arrays of type char, we could not explore string until we had dealt with arrays • Strings are important, because we also have to manipulate textual data as well as numerical data

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

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

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

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

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

  9. 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; }

  10. String Input/Output (Alternate functions) #include <stdio.h> #define MAXLENGTH 15 int main() { char string1[MAXLENGTH]; char string2[MAXLENGTH]; gets(string1); gets(string2); puts(string1); puts(string2); return 0; }

  11. Character Strings • Terminating Character: • Marks the end of string • Special char:’\0’ • aka NUL (single L) Initialization: char name[5] = “Ali”; name is 0x2000 A l i \0 0x2000 0x2004

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

  13. Character Strings Takes up an extra cell for ‘\0’ Declaration 3: char name[] = “Ali”; name is 0x2000 A l i \0 0x2000 0x2003

  14. Character Strings Result is “undefined” if you try to modify this string. Declaration 4: char *name = “Ali”; 0x3000 A l i \0 name 0x3000 0x3003

  15. Character Strings Declaration 5: char name[]; String with arbitrary length? No! Will cause an error.

  16. Arrays of Strings • One string is an array of characters, so an array of strings is a two-dimensional array of characters in which each row is a string • Example • char days[7][10] = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

  17. A Char in a String • The size of a character string is fixed. • Character at position index: • string[index] • first character has index 0

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

  19. A Char in a String 0x3995 0x399C name is 0x3995 A i s e \0 index 2 char name[8] = “Aise”; name[2] = ‘X’; printf(“Name: %s\n”, name);

  20. A Char in a String 0x3995 0x399C name is 0x3995 A i X e \0 index 2 char name[8] = “Aise”; name[2] = ‘X’; printf(“Name: %s\n”, name); output:Name: AiXe

  21. String Operations • #include <string.h> • Operations: • Assignment:strcpy() • Concatenation: strcat() • Comparison: strcmp() • Length:strlen()

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

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

  24. 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!”

  25. Common Mistake: Wrong Assignment Example 1: char name1[5] = “Ali”; char name2[5] = “Sami”; name2 = name1; Error:“LValue required....”

  26. Common Mistake: Bad Assignment Example 2: char *name1 = “Ali”; char *name2 = “Sami”; name2 = name1; Better avoid initialising strings this way. (Usually, no error message.)

  27. 0x2000 A l i \0 name1 0x2000 0x2003 0x3990 S a m i \0 name2 0x3990 0x3994 Common Mistake: Bad Assignment char *name1 = “Ali”; char *name2 = “Sami”;

  28. Common Mistake: Bad Assignment name2 = name1; 0x2000 A l i \0 name1 0x2000 0x2003 0x2000 S a m i \0 name2 0x3990 0x3994

  29. Common Mistake: Not enough space char name[] = “Ali”; strcpy(name, “Samir”); name is 0x2000 A l i \0 0x2000 0x2003

  30. Common Mistake: Not enough space Requires caution. char name[] = “Ali”; strcpy(name, “Samir”); name is 0x2000 S a m i r \0 0x2000 0x2003

  31. 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 “

  32. 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 “

  33. 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 “

  34. 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 “

  35. Common Mistake: Not enough space char name[5]; strcpy(name, “Ali”); strcat(name, “ Osman”); name is 0x2000 A l i \0 0x2000 0x2004

  36. Common Mistake: Not enough space char name[5]; strcpy(name, “Ali”); strcat(name, “ Osman”); name is 0x2000 A l i O s m a n \0 0x2000 0x2004

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

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

  39. 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); }

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

  41. name is 0x3990 O s m a n \0 0x3990 0x3994 Common Mistake: Not enough space char name[5]; strcpy(name, “Osman”); Don’t forget the ‘\0’.

  42. Character Strings as Parameters • Strings as formal parameters are declared as char* or char[] • Examples: void Greet ( char* name ) void Greet ( char name[] ) • As pointer to the first element of the string (array of chars). • Changes to the string inside the function affect the actual string.

  43. user Ali Example: hello3.c #include <stdio.h> #include <string.h> #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; }

  44. Example: hello3.c #include <stdio.h> #include <string.h> #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } name user Ali\0

  45. Example: hello3.c #include <stdio.h> #include <string.h> #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } name user Ali! How are you?\0

  46. Example: hello3.c #include <stdio.h> #include <string.h> #define NAMELEN 50 /* Print a simple greeting to the user. */ void Greet ( char * name ) { strcat(name,"! How are you?"); } int main() { char user[NAMELEN]; printf("Who are you? "); scanf("%s", user); Greet(user); printf("%s\n", user); return 0; } user Ali! How are you?\0

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

  48. sscanf function • sscanf function works exactly like scanf except that instead of taking the data values for its output parameters from the standard input device, it takes data from the string that is its first argument • Example sscanf(“85 96.2 hello”,”%d%lf%s”,&n,&val, word);

  49. Summary • A string is a contiguous array of chars • 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 and finding lengths of strings

More Related