15 - ICS103 Programming in C Lecture 15 Strings 1 Outline What is a String The NULL Character `\\0 in Strings Input\/Output with printf and scanf

15 - ICS103 Programming in C Lecture 15 Strings 1 Outline...

This preview shows page 1 - 7 out of 28 pages.

1 ICS103 Programming in C Lecture 15: Strings
Image of page 1
2 Outline What is a String? The NULL Character ‘\0’ in Strings Input/Output with printf and scanf Input/Output with gets and puts Other String functions in the standard Library strcat, strcpy, strcmp, strlen, strchr, strstr, strtok Character related functions isalpha, isdigit, islower, isupper, toupper, tolower, …
Image of page 2
3 What is a String? A string is any sequence of characters enclosed in double quotes -- "Salam Shabab". There is no separate data type for strings as char , integer , float or double . Instead, a string is represented in C as an array of type char . We have already used string constants extensively in our earlier work: printf ( " The result is: %d\n " , result);   The format string, " The result is: %d\n " is a string constant We can declare and initialize a string variable using any of the following: char str1[20] = {'S','a','l','a','m',' ','S','h','a','b','a','b‘,’\0’}; //as other arrays char str2[20] = "Salam Shabab"; //with size char str3[]= "Salam Shabab"; //without size char *str4= "Salam Shabab"; // as a pointer (arrays are pointers)
Image of page 3
4 The NULL Character ‘\0’ in Strings In all our array processing examples so far, for the processing to be done correctly, in addition to the array, we always had to provide the size of the array as well. e.g. void print_array(int a[], int size ) To avoid the need for size, when we initialize a string, C automatically appends the special NULL character, ‘\0’, to the end of the string to help identify the actual end of the string. For example, the declaration: char str[20] = "Salam Shabab"; is actually represented in the memory as shown below: The implication of this is that when we specify the size of a string in a declaration, we must make sure it is big enough to take the NULL character as well. This is the only way functions like printf can know the end of the string.
Image of page 4
5 Input/Output with printf and scanf Both printf and scanf can handle string arguments as long as the placeholder %s is used in the format string: char s[] = “Salam Shabab”; printf(”%s\n”, s); The printf function, like other standard library functions that take string arguments, depends on finding a null character in the character array to mark the end of the string. If printf were passed a character array that contained no ’\0’, the function would continue to display as characters the content of memory locations following the array argument until it encountered a null character or until it attempted to access a memory cell that was not assigned to the program, causing a run-time error .
Image of page 5
6 Input/Output with printf and scanf … The approach scanf takes to string input is very similar to its processing of numeric input.
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 28 pages?

  • Spring '10
  • baleh
  • ASCII, null character

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes