1 / 86

C Programming

Learn about strings in C programming, including how to initialize them, the string terminator, reading strings from user input, and comparing strings.

jpearl
Télécharger la présentation

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 Strings

  2. Strings • An array of characters • Used to store text • Two ways to initialize: char str[] = "Text"; Instead of: char str[] = {‘T’,’e’,’x’,’t’};

  3. …. 's' '#' ' ' 'f' 'd' 'y' '4' '7' '$' '_' 'e' 'g' 'd' '.' 'p' 'v' …. …. 'H' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' 'g' 'd' '.' 'p' 'v' …. Where does it end? …. 'H' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '\0' 'd' '.' 'p' 'v' …. str Terminator

  4. The Terminator • Strings terminate with the '\0' character (ascii code 0) • This is a convention used to know where the string ends • It means that in order to hold a string of N characters we need an array of length N + 1 • So: char str[] = "Text"; Is equal to: char str[] = {'T', ‘e', ‘x', ‘t', '\0'};

  5. What does it print? int main(void) { char str[] = "I'm a full string"; printf("%s\n", str); str[7] = 'o'; str[8] = 'o'; printf("%s\n", str); str[10] = '\0'; printf("%s\n", str); str[10] = 's'; printf("%s\n", str); return 0; } 0 1 2 3 4 5 6 7 8 9 10 11… I’m a full string I’m a fool string I’m a fool I’m a foolsstring

  6. Reading strings • There are several ways of accepting strings as input from the user • One way is to read character by character using getchar()

  7. Example – Using getchar() #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; /* We need one more place for the '\0' */ char c; int i; printf("Please enter a string:\n"); i = 0; c = getchar(); while (c >= 0 && c != '\n' && i < MAX_LENGTH) { str[i] = c; ++i;c = getchar(); } str[i] = '\0'; /* Terminate the string */ printf("The string you entered is: %s\n", str); return 0; }

  8. Reading strings: scanf • Another way is to use scanf • To read in a string to a variable str, write : scanf("%s", str); Note there is no ‘&’ sign!!!

  9. Reading strings - scanf • scanf reads in letters until a space or newline ('\n') is encountered • The maximum length can be stated in the parentheses: • scanf("%10s", str); • This will read 10 letters, plus the '\0' sign (so str should be of size at least 11)

  10. Example – using scanf #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; printf("Please enter a string:\n"); scanf("%20s", str); printf("The string you entered is: %s\n", str); return 0; }

  11. scanf problem • After using scanf the next character that will be read is the space or newline. • For example:scanf("%s", str);scanf("%c", &tav);Here tav has the value ‘ ’ or newline (‘\n’).

  12. Solving the problem • We need to read and discard the unwanted newline. • Either use getchar orinform scanf to expect spaces (also newline) before the next character.scanf("%s", str);scanf(" %c", &tav);

  13. Comparing strings • We cannot just compare strings’ contents by == char A[6]=“Hello”; char B[6]=“Hello”; if (A==B) { ... } • Because A and B are addresses of A[0] and B[0] • A==B only if A and B are the same string in memory • In order to compare the contents we must scan char by char …. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ …. …. …. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ …. …. A B

  14. Comparing Strings - Example int i; char A[101], B[101]; printf("Enter first string\n"); scanf("%100s",A); printf("Enter second string\n"); scanf("%100s",B); for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]){ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n");

  15. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 0

  16. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 0

  17. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

  18. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

  19. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

  20. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

  21. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

  22. Compare – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

  23. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 0

  24. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 0

  25. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

  26. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

  27. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

  28. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

  29. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

  30. Equal strings – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

  31. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 0

  32. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 0

  33. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 1

  34. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 1

  35. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 2

  36. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 2

  37. Different length – step by step A for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; ‘Y’ ‘e’ ‘s’ ‘\0’ B ‘Y’ ‘e’ ‘\0’ i 2

  38. Exercise • implement the function replacevoid replace(char str[], char what, char with); • The function scans the string and replaces every occurrence of the first char with the second one. • write a program to test the above function • the program should read a string from the user (no spaces) and two characters, then call the function with the input, and print the result. • example Please enter a string (no spaces) papa Letter to replace: p Letter to replace with: m The result: mama

  39. Solution void replace(char str[], char replace_what,char replace_with) { int i; for (i = 0; str[i] != '\0'; ++i) { if (str[i] == replace_what) str[i] = replace_with; } }

  40. Solution #define STRING_LEN 100 int main(void) { char str[STRING_LEN + 1]; char replace_what, replace_with; printf("Please enter a string (no spaces)\n"); scanf("%100s", str); printf("Letter to replace: "); scanf(" %c", &replace_what); printf("Letter to replace with: "); scanf(" %c", &replace_with); replace(str, replace_what, replace_with); printf("The result: %s\n", str); return 0; }

  41. String library • Like in the case of stdio.h and math.h, we have a special library for handling strings • We should #include<string.h>

  42. String library • All functions assume that a string ends with ‘\0’. • Useful functions: • int strlen(const char s[])returns the length of s • int strcmp(const char s1[], const char s2[])compares s1 with s2 Returns 0 if they are equal, -1 if s1<s2, 1 if s1>s2 • strcpy(char s1[], const char s2[])copies to contents of s2 to s1 • and more…

  43. ctype library • #include <ctype.h> • Useful operation on single characters • int isalpha(char c); • int islower(char c); • int isupper(char c); • int isdigit(char c); • char tolower(char c); • char toupper(char c);

  44. Exercise • Implement the following function:int evaluate(char expr[]); • ‘evaluate’ calculate the value of a mathematical expression comprised of positive numbers and the operations ‘+’ and ‘-’ • For example: Input: “7+2-3+5” Output: 11 • You may assume that the string is valid and doesn’t contain spaces. • Also, that all numbers are single digit • Test your function!

  45. Guidance • Read a number and according to the operation add or subtract from the current result. • Use function int char2int(char c) in ctype.h • How do we handle the first number? last one?

  46. Solution int evaluate(char expr[]) { char op = '+'; int i, res = 0; i = 0; while (expr[i] != '\0') { if (op == '+') { res += char2int(expr[i]); } else { res -= char2int(expr[i]); } ++i; op = expr[i]; if (op != '\0') ++i; } return res; }

  47. C Programming - Structures

  48. Structures • Often we want to be able to manipulate ‘logical entities’ as a whole • For example, complex numbers, dates, student records, etc. • A logical entity is often composed of more than one variable

  49. Structures • A struct (short for structure) is a collection of variables of different types, gathered into one super-variable • It is used to define more complex data types • Variables in a struct are called members or fields

  50. Example – complex numbers. The following is the definition of a new ‘variable’ of type complex number: • struct complex { int real; int img; }; • Once we define a structure, we can treat it as any type. • In a program, we can then write: structcomplex num1, num2, num3;

More Related