Lec 11
What’s a String ? A string is a data structure that deals with the grouping of characters. In C, strings are implemented using arrays of type char.
Declaring and Initializing Strings Declaration char str_var[20]; /*declaring a string variable called str_var */ Initialization char str_var[20]={'A', 'p', 'p', 'l', 'e', ' ', 'a', 'n', 'd', ' ', 'P', 'e', 'a', 'r', 's'}; char str_var[20]="Apples and Pears";
Single and Double Quotes Single quotations are used to assign a character to a variable. Example: if s is declared as char s; then the assignment statement for s is s=‘L’; Double quotations are used to assign a string to a variable. Example: if s is declared as char s[10]; then the assignment statement for s is s="Computers";
Arrays of Strings Arrays of Strings is a two-dimensional array containing one character in each element. Example: char months[12][10]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; char names[4][6]={"Sara", "Nora", "Nada", "Moudy"};
Definitions Null character The null character is represented by the character '\0'. This character marks the end of the string in C. White space characters Blanks (space bar), new lines (enter), and tabs. The %s placeholder Used in scanf’s and printf’s to print a string.
Initializers and Strings If number of initializers are less than the number of elements, the null character is added after the last character. char str_var[20]="Apples and Pears"; If number of initilaizers is more than the number of elements, a compiler error occurs. char x[4]="Hello"; If number of initilaizers equals the number of elements, a compiler overflow error occurs. char x[7]="Hangman";
scanf’s and Strings C o m p u t e r s \0 x scanf("%s", &x); /* word */ gets(x); /* word or sentince */ for (i=0; i<9; i++) scanf("%c", &x[i]); x[9]='\0'; Because the user cannot enter a null in a loop
scanf’s and Strings When scanf encounters whitespace characters before the string it skips these characters. When scanf encounters whitespace characters after the string it stops scanning and the null charcter is placed at the end.
printf’s and Strings C o m p u t e r s \0 x for (i=0; i<9; i++) printf("%c", x[i]); or for (i=0; x[i]!='\0'; i++) printf(" %c", x[i]); printf("%s", x);
Right and Left Justification Char x[7] = "Hello!"; Left Justification printf("%-20s", x); Right Justification printf("%20s", x); Hello! Hello!
String Manipulation Functions strcpy(x,y); strcpy: Makes a copy of the second argument into the first argument. It automatically adds the null to the end of the first argument. Examples: strcpy(x, "Hi"); strcpy(z, x); destination Source
String Manipulation Functions strncpy(x,y,n); strncpy: Makes a copy of up to n characters from the second argument to the first. It does not add the null. It can be added manually. Examples: strncpy(x, "Hi", 1); strncpy(z, x, 1);
Example /* a program to demonstrate string assignments */ #include<stdio.h> #include<string.h> int main(void) {char x[]="Happy Birthday to You"; char y[25], z[15]; printf("The string in array x is: %s\n", x); strcpy(y,x); printf("The string in array y is: %s\n", y); strncpy(z, y, 14); z[14]='\0'; printf("The string in array z is: %s\n", z); return(0);}
Example /* a program that divides a string into substrings */ #include<stdio.h> #include<string.h> int main(void) {char last[20], first[20], middle[20]; char name[20]="Lama Bandar Fahad"; strncpy(first, name, 4); first[4]='\0'; strncpy(middle, &name[5], 6); middle[6]='\0'; strcpy(last, &name[12]); printf("%s %s %s\n", last, first, middle); return(0);}
String Manipulation Functions strlen(x); strlen: Returns an integer number representing the number of characters in a string not including the null character. Examples: i=strlen("Hi"); 2 printf("%i", strlen("Hello")); 5
String Manipulation Functions strcat(x,y); strcat: Adds the string in the second argument to the end of the first. Examples: strcat(x, "!");
String Manipulation Functions strncat(x,y,n); strncat: Adds n characters of the string in the second argument to the end of the first one. Examples: strncat(x, "the end", 3);
String Manipulation Functions strcmp(x,y); strcmp: compares x and y. It returns a: 0 if x is equal to y. 1 if x is grater than y. -1 if x is less than y. Examples: #include<stdio.h> #include<string.h> int main(void) {char x[20]="universe"; char y[20]="university"; printf("%i\n", strcmp(x, y)); return(0);}
String Manipulation Functions Summary: strcpy(x,y); /* string assignment */ strncpy(x,y,n); strlen(x); /* determining string length */ strcat(x,y); /* appending to a string */ strncat(x,y,n); strcmp(x,y) /* comparing two strings */
Review (String Manipulation Functions) char x[10]="The", y[5]="End!"; int n=3; strcpy(x,y) strncpy(x,y,n) strlen(x) strcat(x,y) strncat(x,y,n) strcmp(x,y) x = "End!" x = "End" 3 x = "TheEnd!" x = "TheEnd" 1