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

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 15: Strings
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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, …
Background 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)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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.
Background 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 .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Input/Output with printf and scanf … The approach scanf takes to string input is very similar to its processing of numeric input. When it scans a string, scanf skips leading whitespace
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online