Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.

Similar presentations


Presentation on theme: "1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series."— Presentation transcript:

1 1 Chapter 10 Strings and Pointers

2 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series of characters surrounded by double quotes.  How to declare a variable to store string values? Represent a string using a one-dimensional array of type char char string[size]; Question: The size of a character array is fixed, how can this variable take string constants with different lengths as values?

3 3 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

4 4 The End-of-String Sentinel \0  A string is  a one-dimensional array of type char. char w[100];  character value \0 is used to terminate a string strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array The size of the string must include the storage needed for the null character \0.

5 5 The End-of-String Sentinel \0  Example: #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w); } % a.out ABC #include int main(void){ char w[100]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; w[4]=‘D'; printf("%s\n", w); } % a.out ABC the null character value \0 is used to terminate a string

6 6 The End-of-String Sentinel \0  Example: #include int main(void){ char w[3]; w[0]='A'; w[1]='B'; w[2]='C'; w[3]='\0'; printf("%s\n", w); } overrun the bounds of w The size of the string must include the storage needed for the null character \0.

7 7 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

8 8 Using scanf to reading string  Using scanf to read in a string  scanf(“%s”, w); read in non-white space characters opositions the input stream to an initial non-white space character oread in non-white space characters oThe process stops when a white space character or EOF is encountered. a null character is placed in memory to end the string.

9 9 Using scanf to reading string #include int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w); } % a.out Enter strings Hello % a.out Enter strings Hello World Hello scanf(”%s”,w);  read in non-white space characters positions the input stream to an initial non-white space character read in non-white space characters The process stops when a white space character or EOF is encountered.  a null character is placed in memory to end the string.

10 10 Outline  String:  Representation of a string: \0  Using scanf to reading string  Initilization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

11 11 Initialization of Strings  Initialization of Strings  Example: initialize a string variable as “abc” char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; char s[]=“abc”; #include int main(void){ char w[]="abc"; printf("%d\n", sizeof(w)); } % a.out 4 The size of the string must include the storage needed for the null character \0.

12 12 Initialization of Strings  A pointer to char can also be initialized with a constant string.  A string constant is stored in memory by the compiler.  the pointer is assigned the address of the constant string in memory.  Example: char p* = “abc”; #include int main(void){ char *p="abc"; printf("%s\n",p); } % a.out abc

13 13 Initialization of Strings  Difference between  initializing an array with a constant string and  initializing a pointer with a constant string #include int main(void){ char s[]="abcdefg"; char *p="abcdefg"; printf("%s\n",s); printf("%s\n",p); printf("%d\n",sizeof(p)); printf("%d\n",sizeof(s)); } 4 bytes is used to represent a memory address % a.out abcdefg 4 8

14 14 Initialization of Strings  Difference between  initializing an array with a constant string the array contains the individual characters followed by the null character  initializing a pointer with a constant string A string constant is stored in memory by the compiler. the pointer is assigned the address of the constant string in memory.

15 15 Examples: Process a string using array notation with subscripts #include int main(void){ char c, name[100]; int i; printf("Enter a Message:\n"); for (i=0; (c=getchar())!='\n'; ++i) name[i]=c; name[i]='\0'; for (i=0; name[i]!='\0'; ++i) { if(isupper(name[i])) name[i]=tolower(name[i]); else if(islower(name[i])) name[i]=toupper(name[i]); } printf("\n%s\n", name); } % a.out Enter a Message: Have a Good Day! hAVE A gOOD dAY!

16 16 Examples: Process a string using pointer #include int main(void){ char c, name[100], *p; int i; printf("Enter a Message:\n"); for (i=0; (c=getchar())!='\n'; ++i) name[i]=c; name[i]='\0'; for (p=name; *p!='\0'; ++p) { if(isupper(*p)) *p=tolower(*p); else if(islower(*p)) *p=toupper(*p); } printf("\n%s\n", name); } % a.out Enter a Message: Have a Good Day! hAVE A gOOD dAY!

17 17 Examples: Process a string using pointer #include int main(void){ char s[] = "Hello World"; printf("%s\n", s); printf("%s\n", s+1); printf("%s\n", s+2); } % a.out Hello World ello World llo World

18 18 Outline  String:  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

19 19 String-Handling Functions in the Standard Library  String-handling functions:  Function prototypes are provided by string.h #include  Functions: Concatenate two strings: strcat (s1, s2); Compare two strings: int strcmp (s1, s2); Copy s2 to s1: strcpy (s1, s2); Length of a string: strlen (s);

20 20 String-Handling Functions in the Standard Library  char &strcat (char *s1, const char *s2); Concatenates s1 and s2, the result is put in s1. The string s1 is returned. #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; strcat(s1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcat(s1, s2+6); printf("s2=%s, s1= %s\n", s2, s1); } % a.out s2=Hello World, s1= Good DayHello World s2=Hello World, s1= Good DayHello WorldWorld

21 21 String-Handling Functions in the Standard Library  int strcmp (const char &s1, const char *s2); An integer is returned that is less than, equal to, or greater tan zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2 #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; printf("%d\n", strcmp(s1, s2)); printf("%d\n", strcmp(s1, s1)); printf("%d\n", strcmp(s2, s1)); } % a.out 0 1

22 22 String-Handling Functions in the Standard Library  char *strcpy (char *s1, const char *s2); s2 is copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The value s1 is returned. #include int main(void){ char s1[100] = "Good Day"; char s2[100] = "Hello World"; strcpy(s1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcpy(s1+1, s2); printf("s2=%s, s1= %s\n", s2, s1); strcpy(s1+1, s2+6); printf("s2=%s, s1= %s\n", s2, s1); } % a.out s2=Hello World, s1= Hello World s2=Hello World, s1= HHello World s2=Hello World, s1= HWorld

23 23 String-Handling Functions in the Standard Library  unsigned strlen (const char *s); A count of the number of characters before \0 is returned. #include int main(void){ char s1[100] = "Good Day"; printf("%d\n", strlen(s1)); } % a.out 8

24 24 Outline  String:  String-Handling Functions in the Standard Library  Passing Arguments to main() using an array of strings

25 25 Passing Arguments to main()  How main() communicates with the operating system?  int main(void)  int main( int argc, char *argv[]) argc: the number of the command line arguments argv: an array of strings

26 26 Passing Arguments to main() #include int main(int argc, char *argv[]){ int i; printf("%d \n", argc); for (i=0; i < argc; ++ i) printf("%s\n", argv[i]); } %a.out Hello World 3 a.out Hello World

27 27 Summary  String:  Representing a string using an array of characters  \0 is used to terminated a string  strings have a variable length delimited by the null character \0 but with a maximum length determined by the size of the character array  initialization of strings  String-Handling Functions in the Standard Library  Passing Arguments to main()  argc: number of arguments  argv: an array of strings


Download ppt "1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series."

Similar presentations


Ads by Google