Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Strings.

Similar presentations


Presentation on theme: "Lecture 11 Strings."— Presentation transcript:

1 Lecture 11 Strings

2 Introduction Until now Strings: printf("This printf("This
We have seen strings in printf Our old definition: string is a set of char between “” printf("This printf("This is a string\n"); is %s\n", "a string\n"); Strings: An array of chars Terminated by the null char '\0'

3 Strings in C Since strings are array char str3[] = {'p', 'r', 'o',
'm', '\0'}; char char str1[8] = "program"; str2[] = "program"; 'p' 'r' 'o' 'g' 'a' 'm' '\0'

4 String Initializing char array ...
char s[10] = "unix"; /* s[4] is '\0'; */ char s[ ] = "unix"; /* s has five elements */

5 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

6 Strings Each character has an integer representation a b c d e … … … …
z ………………………112 A B C D E Z ……………………… 90 1 2 3 4 5 6 7 8 9 \0 \n 10

7 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] = '\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]) ... ? \0 ! i h [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] s

8 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);

9 Reading & Writing Strings
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 printf knows how much to print out because of the NULL character at the end of all strings When it finds a \0, it knows to stop

10 Example char str[11]="unix and c"; printf("%s", str); printf("\n");
printf(str); str[2]='I'; \0 c d n a x i u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str \0 c d a x i n u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str \0 c d a x I n u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str

11 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

12 Solution

13 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

14 Solution

15 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, 0 if S1 == S2 a pos value if S1 > S2 H e l l o \0 H e l o o \0 l < o in lexicographic order

16 Solution (incomplete)

17 Solution (complete)

18 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 Reading & Writing Strings (cont’d)
gets(char str[]) can be used to read strings gets does not ignore the white spaces Read until \n

20 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 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 Length of String strlen(str): Length of string
From start to first occurrence of the null char char char str[] = "This is test"; str1[10]={'a', 'b', '\0', 'c', '\0'}; strlen(str)  12 strlen(str1)  2

23 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 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 Compare Strings strcmpi(str1, str2)
Compares str1 and str2 similar to strcmp But ignores uppercase/lowercase difference char str1[]="ABC", str2[]="abC"; strcmpi(str1, str2)  0

26 strcmp Compares n chars of str1 and str2
int strncmp (char *str1, char * str2, size_t n); Compares n chars of str1 and str2 Continues until n chars are compared or The end of str1or str2 is encountered

27 Example char str1[] = "The first string.";
char str2[] = "The second string."; printf("%d\n", strcmp(str1,str2) ); // -1 printf("%d\n", strncmp(str1,str2,4) ); // 0 // 'f' - 's' = -13 printf("%d\n", strncmp(str1,str2,5) ); // -13 If(str1 == str2) … // Syntax error in many c complier!

28 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)

29 Copy Strings: Example char str1[] = "Test String"; char str2[20];
strcpy(str2, str1); printf("%s\n", str2); printf("%s\n", str1); Test String Test String

30 strcat Appends a copy of str2 to the end of str1
Concatenating two stings: char * strcat (char * str1, char * str2); Appends a copy of str2 to the end of str1 Ensure that str1 has sufficient space for the concatenated string! Array index out of range will be the most popular bug in your C programming career

31 Concatenate Strings: Example
strcat(str2, str1); printf("%s\n", str2); char str1[] = "String"; char str2[20]= "Test "; Test String

32 Example

33 Common Bugs & Avoiding them
Strings which are used as destination scanf, strcpy, …. Must be large enough Take care about the ‘\0’ You should never destroy it!

34 Quiz 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


Download ppt "Lecture 11 Strings."

Similar presentations


Ads by Google