860 likes | 870 Views
Learn about strings in C programming, including how to initialize them, the string terminator, reading strings from user input, and comparing strings.
E N D
C Programming Strings
Strings • An array of characters • Used to store text • Two ways to initialize: char str[] = "Text"; Instead of: char str[] = {‘T’,’e’,’x’,’t’};
…. '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
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'};
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
Reading strings • There are several ways of accepting strings as input from the user • One way is to read character by character using getchar()
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; }
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!!!
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)
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; }
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’).
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);
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
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");
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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; } }
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; }
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>
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…
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);
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!
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?
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; }
C Programming - Structures
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
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
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;