1 / 27

Chapter 10 Strings and Pointers

Chapter 10 Strings and Pointers. Introduction. String Constant Example: printf(“Hello”); “Hello” : a string constant A string constant is a series of characters surrounded by double quotes. How to declare a variable to store string values?

Download Presentation

Chapter 10 Strings and Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Strings and Pointers

  2. Introduction • String Constant • Example: printf(“Hello”); • “Hello” : a string constant • A 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. 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. 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. The End-of-String Sentinel \0 the null character value \0 is used to terminate a string • Example: #include <stdio.h> 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); } #include <stdio.h> 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 % a.out ABC

  6. The End-of-String Sentinel \0 The size of the string must include the storage needed for the null character \0. • Example: #include <stdio.h> 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

  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. Using scanf to reading string • Using scanf to read in a string • 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.

  9. Using scanf to reading string 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. #include <stdio.h> int main(void){ char w[10]; printf("Enter strings\n", w); scanf("%s", w); printf("%s\n", w); } % a.out Enter strings Hello Hello % a.out Enter strings Hello World Hello

  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. Initialization of Strings • Initialization of Strings • Example: initialize a string variable as “abc” • char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; • char s[]=“abc”; #include <stdio.h> 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. 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 <stdio.h> int main(void){ char *p="abc"; printf("%s\n",p); } % a.out abc

  13. Initialization of Strings • Difference between • initializing an array with a constant string and • initializing a pointer with a constant string #include <stdio.h> 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)); } % a.out abcdefg abcdefg 4 8 4 bytes is used to represent a memory address

  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. Examples: Process a string using array notation with subscripts #include <stdio.h> 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. Examples: Process a string using pointer #include <stdio.h> 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. Examples: Process a string using pointer #include <stdio.h> 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. Outline • String: • String-Handling Functions in the Standard Library • Passing Arguments to main() using an array of strings

  19. String-Handling Functions in the Standard Library • String-handling functions: • Function prototypes are provided by string.h • #include <string.h> • 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. 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 <stdio.h> #include <string.h> 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. 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 <stdio.h> #include <string.h> 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 -1 0 1

  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 <stdio.h> #include <string.h> 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. String-Handling Functions in the Standard Library • unsigned strlen (const char *s); • A count of the number of characters before \0 is returned. #include <stdio.h> #include <string.h> int main(void){ char s1[100] = "Good Day"; printf("%d\n", strlen(s1)); } % a.out 8

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

  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. Passing Arguments to main() #include <stdio.h> 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. 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

More Related