Lecture 17 Strings Department of Computer Engineering 1

  • Slides: 34
Download presentation
Lecture 17 Strings Department of Computer Engineering 1 Sharif University of Technology

Lecture 17 Strings Department of Computer Engineering 1 Sharif University of Technology

Input and Output – Lecture 4 Introduction Until now We have seen strings in

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 stringn"); printf("This is %sn", "a stringn"); Strings: An array of chars Terminated by the null char '' Department of Computer Engineering 2 Sharif University of Technology

Input and Output – Lecture 4 Strings in C Since strings are array char

Input and Output – Lecture 4 Strings in C Since strings are array char str 3[] = {'p', 'r', 'o', 'g', 'r', 'a', 'm', ''}; char str 1[8] = "program"; char str 2[] = "program"; 'p' 'r' 'o' 'g' 'r' 'a' 'm' '' Department of Computer Engineering 3 Sharif University of Technology

Input and Output – Lecture 4 String • Initializing char array. . . –

Input and Output – Lecture 4 String • Initializing char array. . . – char s[10] = "unix"; /* s[4] is ''; */ – char s[ ] = "unix"; /* s has five elements */ Department of Computer Engineering 4 Sharif University of Technology

Input and Output – Lecture 4 Strings are Character Arrays • Strings in C

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 '' to terminate all strings in its library functions • For example: char str [10] = {'u', 'n', 'I', 'x', ''}; • It’s the string terminator (not the size of the array) that determines the length of the string Department of Computer Engineering 5 Sharif University of Technology

Input and Output – Lecture 4 Strings • Each character has an integer representation

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 65 66 67 0 1 2 D E … … Z 68 69 …………… 90 3 4 5 6 7 8 9 48 49 50 51 52 53 54 55 56 57 0 n 10 Department of Computer Engineering 6 Sharif University of Technology

Input and Output – Lecture 4 Accessing Individual Characters • The first element of

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'; s[1] = 'i’; s[2] = '!'; s[3] = ''; h s i ! ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] ? [9] • 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]). . . Department of Computer Engineering 7 Sharif University of Technology

Input and Output – Lecture 4 Strings • Characters can be interpreted as integers

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 Department of Computer Engineering 8 Sharif University of 8 Technology

Reading & Writing Strings Input and Output – Lecture 4 printf can be used

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 ) We must allocate sufficient size Sharif University of Technology Department of Computer Engineering 9

Input and Output – Lecture 4 Example char str[11]="unix and c"; printf("%s", str); printf("n");

Input and Output – Lecture 4 Example char str[11]="unix and c"; printf("%s", str); printf("n"); str[6]=''; printf("%s", str); printf("n"); printf(str); printf("n"); str[2]='I'; printf(str); printf("n"); u n i x a n d str [0] [1] [2] [3] [4] [5] [6] [7] [8] u n i x a d str [0] [1] [2] [3] [4] [5] [6] [7] [8] u n I x a d str [0] [1] [2] [3] [4] [5] [6] [7] [8] Department of Computer Engineering 10 c [9] [10] Sharif University of Technology

Input and Output – Lecture 4 Exercise • Write a function to count the

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 H e l l Department of Computer Engineering o 11 Sharif University of Technology

Input and Output – Lecture 4 Solution int count_letters(char cdata[]) { int i=0; while

Input and Output – Lecture 4 Solution int count_letters(char cdata[]) { int i=0; while (cdata[i] != '') i = i + 1; return(i); } Department of Computer Engineering 12 Sharif University of Technology

Input and Output – Lecture 4 Exercise • Write a function that prints a

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 Output: olle. H Department of Computer Engineering 13 Sharif University of Technology

Input and Output – Lecture 4 Solution void print_reverse(char pdata[]) { int size, position;

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; } Department of Computer Engineering 14 Sharif University of Technology

Input and Output – Lecture 4 Exercise • Write a function that compares 2

Input and Output – Lecture 4 Exercise • Write a function that compares 2 strings S 1 and S 2 using lexicographic order. • Idea: compare character by character • Return – a neg value if S 1 < S 2, – 0 if S 1 == S 2 – a pos value if S 1 > S 2 H e l l o H e l o o l < o in lexicographic order Department of Computer Engineering 15 Sharif University of Technology

Input and Output – Lecture 4 Solution (incomplete) int compare(char cdata 1[], char cdata

Input and Output – Lecture 4 Solution (incomplete) int compare(char cdata 1[], char cdata 2[]) { int i= 0; while (cdata 1[i] == cdata 2[i]) i = i + 1; return (cdata 1[i] - cdata 2[i]); } Department of Computer Engineering 16 Sharif University of Technology

Input and Output – Lecture 4 Solution (complete) int compare(char cdata 1[], char cdata

Input and Output – Lecture 4 Solution (complete) int compare(char cdata 1[], char cdata 2[]) { int i= 0; while (cdata 1[i] != ‘’ && cdata 2[i] != ‘’ && cdata 1[i] == cdata 2[i]) i = i + 1; return (cdata 1[i] - cdata 2[i]); } Department of Computer Engineering 17 Sharif University of Technology

Input and Output – Lecture 4 Exercise: Spell out a number in text using

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 Department of Computer Engineering 18 Sharif University of Technology

Input and Output – Lecture 4 Reading & Writing Strings (cont’d) puts(str)is very simple

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 foxn"); n Department of Computer Engineering 19 Sharif University of Technology

Input and Output – Lecture 4 Reading & Writing Strings (cont’d) Gets (str)can be

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 Department of Computer Engineering 20 Sharif University of Technology

Input and Output – Lecture 4 Difference between gets and scanf • gets( )

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]; gets(line); puts(line); Department of Computer Engineering char line[80]; scanf("%[ ^n]s", line); printf(“%sn", line); 21 Sharif University of Technology

Input and Output – Lecture 4 String Library Access to string library by #include

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 Department of Computer Engineering 22 Sharif University of Technology

Input and Output – Lecture 4 Length of String strlen(str): Length of string From

Input and Output – Lecture 4 Length of String strlen(str): Length of string From start to first occurrence of the null char str[] = "This is test"; char str 1[10]={'a', 'b', '', 'c', ''}; strlen(str) 12 strlen(str 1) 2 Department of Computer Engineering 23 Sharif University of Technology

Input and Output – Lecture 4 Compare Strings str 1 and str 2 are

Input and Output – Lecture 4 Compare Strings str 1 and str 2 are compared as follows Compare char by char from left to right until str 1 and str 2 has same chars. In the first different char If(char of str 1 < char of str 2) str 1 < str 2 If (both string finish) str 1 = str 2 strcmp(str 1, str 2): compare str 1 and str 2 If(str 1 == str 2) return 0 If(str 1 < str 2) return -1 If(str 1 > str 2) return 1 Department of Computer Engineering 24 Sharif University of Technology

Input and Output – Lecture 4 Compare Strings: Examples char s 1[] = "abc";

Input and Output – Lecture 4 Compare Strings: Examples char s 1[] = "abc"; char s 2[] = "abc"; i = strcmp(s 1, s 2); //i = 0 //i = -1 //i = 1 char s 3[] = "abc"; char s 4[] = "abx"; i = strcmp(s 3, s 4); char s 5[] = "axc"; char s 6[] = "abc"; i = strcmp(s 5, s 6); char s 7[] = "ab"; char s 8[] = "abc"; i = strcmp(s 7, s 8); //i = -1 //i = 1 char s 9[] = "abc"; char s 10[] = "a. Bc"; i = strcmp(s 9, s 10); Department of Computer Engineering 25 Sharif University of Technology

Input and Output – Lecture 4 Compare Strings strcmpi(str 1, str 2) Compares str

Input and Output – Lecture 4 Compare Strings strcmpi(str 1, str 2) Compares str 1 and str 2 similar to strcmp But ignores uppercase/lowercase difference char str 1[]="ABC", str 2[]="ab. C"; strcmpi(str 1, str 2) 0 Department of Computer Engineering 26 Sharif University of Technology

Input and Output – Lecture 4 strncmp int strncmp (char *str 1, char *

Input and Output – Lecture 4 strncmp int strncmp (char *str 1, char * str 2, size_t n); – Compares n chars of str 1 and str 2 • Continues until n chars are compared or • The end of str 1 or str 2 is encountered Department of Computer Engineering 27 Sharif University of Technology

Input and Output – Lecture 4 Example char str 1[] = "The first string.

Input and Output – Lecture 4 Example char str 1[] = "The first string. "; char str 2[] = "The second string. "; printf("%dn", strcmp(str 1, str 2) ); // -1 printf("%dn", strncmp(str 1, str 2, 4) ); // 0 // 'f' - 's' = -13 printf("%dn", strncmp(str 1, str 2, 5) ); // -13 If(str 1 == str 2) … // Syntax error in many c complier! Department of Computer Engineering 28 Sharif University of Technology

Input and Output – Lecture 4 strcpy • Copying a string comes in the

Input and Output – Lecture 4 strcpy • Copying a string comes in the form: char *strcpy (char * destination, char * source); • A copy of source is made at destination – source should be NULL terminated – destination should have enough room (its length should be at least the size of source) Department of Computer Engineering 29 Sharif University of Technology

Copy Strings: Example Input and Output – Lecture 4 char str 1[] = "Test

Copy Strings: Example Input and Output – Lecture 4 char str 1[] = "Test String"; char str 2[20]; strcpy(str 2, str 1); printf("%sn", str 2); Test String printf("%sn", str 1); Test String Department of Computer Engineering 30 Sharif University of Technology

Input and Output – Lecture 4 strcat • Concatenating two stings: char * strcat

Input and Output – Lecture 4 strcat • Concatenating two stings: char * strcat (char * str 1, char * str 2); – Appends a copy of str 2 to the end of str 1 • Ensure that str 1 has sufficient space for the concatenated string! – Array index out of range will be the most popular bug in your C programming career Department of Computer Engineering 31 Sharif University of Technology

Input and Output – Lecture 4 Concatenate Strings: Example char str 1[] = "String";

Input and Output – Lecture 4 Concatenate Strings: Example char str 1[] = "String"; char str 2[20]= "Test "; strcat(str 2, str 1); Test String printf("%sn", str 2); Department of Computer Engineering 32 Sharif University of Technology

Input and Output – Lecture 4 Example #include <string. h> #include <stdio. h> int

Input and Output – Lecture 4 Example #include <string. h> #include <stdio. h> int main() { char str 1[10] = "abc"; char str 2[100]; } printf("%dn", strlen(str 1)); strcpy(str 2, str 1); puts(str 2); puts("n"); // printf("nn"); strcat(str 2, str 1); puts(str 2); Department of Computer Engineering 33 Sharif University of Technology

Input and Output – Lecture 4 Common Bugs & Avoiding them Strings which are

Input and Output – Lecture 4 Common Bugs & Avoiding them Strings which are used as destination scanf, strcpy, …. Must be large enough Take care about the ‘’ You should never destroy it! Department of Computer Engineering 34 Sharif University of Technology