Strings 1 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Introduction Until now We have seen strings in printf Our old definition: string is a set of char between “” printf("This is a string\n"); printf("This is %s\n", "a string\n"); Strings: An array of chars Terminated by the null char '\0' 2 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Strings in C Since strings are array char str3[] = {'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0'}; char str1[8] = "program"; char str2[] = "program"; 'p' 'r' 'o' 'g' 'r' 'a' 'm' '\0' 3 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 String • Initializing char array ... – char s[10] = "unix"; /* s[4] is '\0'; */ – char s[ ] = "unix"; /* s has five elements */ 4 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Strings are Character Arrays • Strings in C are simply arrays of characters – Example: char s [10]; • This is a ten (10) element array that can hold a character string consisting of 9 characters • This is because C does not know where the end of an array is at run time – By convention, C uses a NULL character '\0' to terminate all strings in its library functions • For example: char str [10] = {'u', 'n', 'I', 'x', '\0'}; • It ’ s the string terminator (not the size of the array) that determines the length of the string 5 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Strings • Each character has an integer representation a b c d e … … … … z 97 98 99 100 101 ……………………… 112 A B C D E … … … … Z 65 66 67 68 69 ……………………… 90 0 1 2 3 4 5 6 7 8 9 48 49 50 51 52 53 54 55 56 57 \0 \n 0 10 6 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Accessing Individual Characters • The first element of any array in C is at index 0. The second is at index 1, and so on ... char s[10]; s[0] = 'h'; h i ! \0 ? ? ? ? ? ? s[1] = 'i ’ ; s [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] s[2] = '!'; s[3] = '\0'; • This notation can be used in all kinds of statements and expressions in C: – For example: c = s[1]; if (s[0] == '-') … switch (s[1]) ... 7 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Strings • Characters can be interpreted as integers char c = ‘ A ’ ; printf( “ %c \n ” ,c); prints A printf( “ %d \n ” ,c); prints 65 Printf( “ %c \n ” ,65); prints A 8 8 Sharif University of Technology Department of Computer Engineering
Reading & Writing Strings Input and Output – Lecture 4 printf can be used to print strings printf("program"); printf("%s", "program"); scanf can be used to read strings char str[200]; scanf("%s", str); Note: No ampersand(&) when inputting strings into character arrays! Initial white spaces are ignored Read until white space (which is replaced by \0) We must allocate sufficient size 9 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Example char str[11]="unix and c"; u n i x a n d c \0 printf("%s", str); str [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] printf("\n"); str[6]='\0'; u n i x a \0 d c \0 printf("%s", str); str [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] printf("\n"); printf("\n"); printf(str); printf("\n"); u n I x a \0 d c \0 str[2]='I'; str [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] printf(str); printf("\n"); 10 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Exercise • Write a function to count the number of characters in a string. • Idea: count the number of characters before \0 H e l l o \0 11 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Solution int count_letters(char cdata[]) { int i=0; while (cdata[i] != '\0') i = i + 1; return(i); } 12 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Exercise • Write a function that prints a string in reverse • Idea: find the end of the string and print the characters backwards. H e l l o \0 Output: olleH 13 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Solution void print_reverse(char pdata[]) { int size,position; size = count_letters(pdata); position = size - 1; while (position >= 0) { printf("%c",pdata[position]); position = position -1; } printf("\n"); return; } 14 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Exercise • Write a function that compares 2 strings S1 and S2 using lexicographic order. • Idea: compare character by character • Return – a neg value if S1 < S2, H e l l o \0 – 0 if S1 == S2 – a pos value if S1 > S2 H e l o o \0 l < o in lexicographic order 15 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Solution (incomplete) int compare(char cdata1[], char cdata2[]) { int i= 0; while (cdata1[i] == cdata2[i]) i = i + 1; return (cdata1[i] - cdata2[i]); } 16 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Solution (complete) int compare(char cdata1[], char cdata2[]) { int i= 0; while (cdata1[i] != ‘ \0 ’ && cdata2[i] != ‘ \0 ’ && cdata1[i] == cdata2[i]) i = i + 1; return (cdata1[i] - cdata2[i]); } 17 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Exercise: Spell out a number in text using arrays of strings • Write a program that reads a number between 1 and 999 from user and spells out it in English. For example: • 453 Four hundred fifty three • 37 Thirty seven • 204 Two hundred four 18 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Reading & Writing Strings (cont ’ d) puts(str) is very simple version of printf Can only be used to print strings Adds ‘ \n ’ to end of string Prototype of puts is defined in stdio.h – This is more efficient than printf : because your program doesn't need to analyze the format string at run-time. – For example: char sentence[] = "The quick brown fox"; puts(sentence); // printf("The quick brown fox \n "); 19 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Reading & Writing Strings (cont ’ d) Gets (str) can be used to read strings Gets does not ignore the white spaces • Read until \n 20 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Difference between gets and scanf • gets( ) read a line • scanf("%s", … ) read up to the next space char line[80]; char line[80]; gets(line); scanf("%[ ^\n]s", line); puts(line); printf( “ %s\n", line); 21 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 String Library Access to string library by #include <string.h> Many functions to work with strings Find the length of string Compare strings Copy strings Search in strings Concatenating strings 22 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Length of String strlen(str) : Length of string From start to first occurrence of the null char char str[] = "This is test"; char str1[10]={'a', 'b', '\0', 'c', '\0'}; 12 strlen(str) strlen(str1) 2 23 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Compare Strings str1 and str2 are compared as follows Compare char by char from left to right until str1 and str2 has same chars. In the first different char If(char of str1 < char of str2) str1 < str2 If (both string finish) str1 = str2 strcmp(str1, str2): compare str1 and str2 If(str1 == str2) return 0 If(str1 < str2) return -1 If(str1 > str2) return 1 24 Sharif University of Technology Department of Computer Engineering
Input and Output – Lecture 4 Compare Strings: Examples char s1[] = "abc"; char s2[] = "abc"; i = strcmp(s1, s2); //i = 0 char s3[] = "abc"; char s4[] = "abx"; i = strcmp(s3, s4); //i = -1 char s5[] = "axc"; char s6[] = "abc"; i = strcmp(s5, s6); //i = 1 char s7[] = "ab"; char s8[] = "abc"; i = strcmp(s7, s8); //i = -1 char s9[] = "abc"; char s10[] = "aBc"; i = strcmp(s9, s10); //i = 1 25 Sharif University of Technology Department of Computer Engineering
Recommend
More recommend