Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings What is a string? It is an array of characters terminated with

Similar presentations


Presentation on theme: "Strings What is a string? It is an array of characters terminated with"— Presentation transcript:

1 Strings What is a string? It is an array of characters terminated with
an extra character '\0' NULL character e.g. char name[5] = {'B','i','l','l','\0'}; equivalent to char name[5] = "Bill"; double quotes Constant string C tacks on '\0' automatically at the end of the string or char name[] = "Bill"; 142 R -1

2 In the memory: name[0] is 'B' name[4] is '\0' tells the computer
where the string ends 142 R -2

3 Using strings do not forget Initialization like arrays of numbers
char name[5] = {'B','i','l','l','\0'}; or char name[5]; name[0]='B'; name[1]='i'; name[2]='l'; name[3]='l'; name[4]='\0'; or char name[5] = "Bill"; particular to string arrays or char name[] = "Bill"; Cannot do char name[5]; /* OK */ name = "Bill"; /* NOT OK */ 142 R -3

4 Input/Output of strings
with scanf char name[5]; scanf("%s",name); placeholder for strings no & (why?) no length check (if name is not big enough, you will get run time problems) skips initial white spaces insert '\0' at the next white space input: CSC142 is a lot of fun name contains 'C', 'S', 'C','1','4','2','\0' with printf printf("%s",name); Specific for strings. There is no placeholder to input or output arrays of numbers. 142 R -4

5 Operations on strings As for arrays of numbers, there are no
operators in C that work directly on strings. Be Careful! char name[5] = "Bill"; /*OK*/ char name2[5]; /*OK*/ name2 = name; /*NOT OK*/ Instead for(i=0; i<5; i++) name2[i] = name[i]; Or more clever! for(i=0; name[i]!='\0'; i++) name2[i] = name[i]; name2[i] = '\0'; don’t need the length of the string But, we don't need to write our own functions. Check the standard string library At the beginning of the program: #include <string.h> 142 R -5

6 Some string functions Copy string s2 into string s1 strcpy(s1,s2);
In C, The C code for strcpy is similar to void strcpy(char s1[], char s2[]) { int i; for(i=0; s2[i]!='\0', i++) s1[i] = s2[i]; s1[i] = '\0'; } Careful, C doesn't check if s2 fits in s1 142 R -6

7 #include <string.h> ... char line1[5];
Don't do: #include <string.h> ... char line1[5]; char line2[]="The Universe" strcpy(line1,line2); Trouble ahead! In the memory T h e U n i v r s '\0' space reserved for line1 just erased some (important?) data 142 R -7

8 When using strcpy, check the length of the strings
How? Use the function strlen char s[]={'H','e','l','l','o','\0'}; strlen(s) is 5 5 because '\0' is not counted When using strcpy, write char s2[] = "Once upon a time"; char s1[SIZE]; if (strlen(s2)<SIZE-1) strcpy(s1,s2); else printf("String is too short!"); 142 R 8

9 Could also copy only the part of s2 that fits in s1
strncpy The C code for strncpy is similar to void strncpy(char dest[], char source[], int destlen) { int i; for(i=0; i<destlen; i++) if (i<=strlen(source)) dest[i] = source[i]; else /* end of dest is padded with nulls */ dest[i] = '\0' } copy at most destlen characters from source to dest Note: '\0' might not be added to dest When? destlen <= strlen(source) 142 R 9

10 String concatenation strcat strcat(s1,s2)
concatenates s2 to the end of s1 e.g. char s1[14]="New York";/*8 characters*/ char s2[]=" city"; /*5 characters*/ strcat(s1,s2); printf("%s",s1); printf prints "New York city" 142 R 10

11 String comparison How to compare strings?
use the (extended) alphabetical order include characters like [, "a" < "d" "ant" < "antelope" "&123" < But, cannot use <, >, == for strings in C Use the function strcmp strcmp(str1,str2) is not C operators <0 if str1 < str2 =0 if str1 = str2 >0 if str1 > str2 142 R 11

12 Pitfalls of strings char str1[] = "Hello"; char str2[] = "Bonjour";
Can we write? str1 = str2 /* ERROR */ if (str1 == str2)... OK, but compare pointers (not the string contents!) if (str1 > str2) OK, but compare pointers (not the string contents!) 142 R 12

13 Arrays of strings Same as multidimensional arrays
char day[7][10]={"Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday","Sunday"}; 10 for Wednesday: 9 characters + 1 (for '\0') Examples printf("CSC142 lab is on %s",day[2]); or char name[NUM_NAMES][MAX_NAME+1]; int age[NUM_NAMES]; int i; for(i=0; i<NUM_NAMES; i++) { scanf("%s%d",name[i],&age[i]); printf("%s %d",name[i],age[i]); } no & 142 R 13

14 Check other functions in <string.h>
strncat, strncmp strtod, strtol, strtoul (to convert a string to a number) See also the related function library for characters: <ctype.h> 142 R 14

15 Summary string: Null terminated array of chars
No string assignment or comparison in C Use built in functions in <string.h> assignment: strcpy, strncpy comparison: strcmp, strncmp length: strlen printf and scanf with %s Pitfall: overrunning the allowed length of a string 142 R 15


Download ppt "Strings What is a string? It is an array of characters terminated with"

Similar presentations


Ads by Google