
Chapter 11 Strings Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏ To understand the design implementation for C-language delimited strings ❏ To write programs that read, write, and manipulate strings ❏ To write programs that use the string functions ❏ To write programs that use arrays of strings ❏ To write programs that parse a string into separate variables ❏ To understand the software engineering concepts of information hiding and cohesion Computer Science: A Structured Programming Approach Using C 1 11-1 String Concepts In general, a string is a series of characters treated as a unit. Computer science has long recognized the importance of strings, but it has not adapted a standard for their implementation. We find, therefore, that a string created in Pascal differs from a string created in C. Topics discussed in this section: Fixed-Length Strings Variable-Length Strings Computer Science: A Structured Programming Approach Using C 2 FIGURE 11-1 String Taxonomy Computer Science: A Structured Programming Approach Using C 3 FIGURE 11-2 String Formats Computer Science: A Structured Programming Approach Using C 4 11-2 C Strings A C string is a variable-length array of characters that is delimited by the null character. Topics discussed in this section: Storing Strings The String Delimiter String Literals Strings and Characters Declaring Strings Initializing Strings Strings and the Assignment Operator Reading and Writing Strings Computer Science: A Structured Programming Approach Using C 5 C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string "Hi there!" would be stored in memory as shown: H i t h e r e ! \0 12-6 A delimiter is a unique character or series of characters that indicates the beginning or end of a specific statement, string or function body set. Note C uses variable-length, delimited strings. Computer Science: A Structured Programming Approach Using C 7 FIGURE 11-3 Storing Strings Computer Science: A Structured Programming Approach Using C 8 FIGURE 11-4 Storing Strings and Characters Computer Science: A Structured Programming Approach Using C 9 FIGURE 11-5 Differences Between Strings and Character Arrays Computer Science: A Structured Programming Approach Using C 10 FIGURE 11-6 Strings in Arrays Computer Science: A Structured Programming Approach Using C 11 Note A string literal is enclosed in double quotes. Computer Science: A Structured Programming Approach Using C 12 FIGURE 11-7 Character Literals and String Literals Computer Science: A Structured Programming Approach Using C 13 FIGURE 11-8 String Literal References Computer Science: A Structured Programming Approach Using C 14 FIGURE 11-9 Defining Strings Computer Science: A Structured Programming Approach Using C 15 Note Memory for strings must be allocated before the string can be used. Computer Science: A Structured Programming Approach Using C 16 char str[9]=""Good Day"; char month[]="January"; char *pstr="Good Day"; char str[9]={'G','o','o','d',' ','D','a','y‘,’\0’}; FIGURE 11-10 Initializing Strings Computer Science: A Structured Programming Approach Using C 17 Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. char s[5]; Strings can also be declared using pointer. char *p Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','\0'}; OR; char c[5]={'a','b','c','d','\0'}; String can also be initialized using pointers char *c="abcd"; Computer Science: A Structured Programming Approach Using C 18 11-3 String Input/Output Functions C provides two basic ways to read and write strings. First, we can read and write strings with the formatted input/output functions, scanf/fscanf and printf/fprintf. Second, we can use a special set of string-only functions, get string (gets/fgets) and put string ( puts/fputs ). Topics discussed in this section: Formatted String Input/Output String Input/Output Computer Science: A Structured Programming Approach Using C 19 Reading Strings from user. Reading words from user: char c[20]; scanf("%s",c); String variable c can only take a word. It is because when white space is encountered, the scanf()function terminates. Write a C program to illustrate how to read string from terminal. #include <stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s",name); printf("Your name is %s.",name); return 0; } Output Enter name: Dennis Ritchie Your name is Dennis. Computer Science: A Structured Programming Approach Using C 20 To accept multiword string from scanf(), following syntax of scanf() is used. Char name[30]; printf(“\n Enter name:”); scanf(“%[^\n]s”, name); To handle multiword strings unformatted i/o can be used. To read multiword string gets() function is used. To print strings puts() function can be used. Computer Science: A Structured Programming Approach Using C 21 Strings and assignment Operator char str1[6]-”hello”; char str2[6]; str2=str1; // compilation error char * s =“C Prog”; char *q; q=s; // works s=“bye”; // works Computer Science: A Structured Programming Approach Using C 22 Strings Library Functions char *strcpy (char *dest, char *src); Copy src string into dest string. char *strncpy(char *string1, char *string2, int n); Copy first n characters of string2 to string1 . int strcmp(char *string1, char *string2); Compare string1 and string2 to determine alphabetic order. int strncmp(char *string1, char *string2, int n); Compare first n characters of two strings. int strlen(char *string); Determine the length of a string. Char * strrev(char *string); Reverses the string. 23 char *strcat(char *dest, const char *src); Concatenate string src to the string dest. char *strncat(char *dest, const char *src, int n); Concatenate n chracters from string src to the string dest. char *strchr(char *string, int c); Find first occurrence of character c in string. char *strrchr(char *string, int c); Find last occurrence of character c in string. char *strstr(char *string2, char string*1); Find first occurrence of string string1 in string2. char *strtok(char *s, const char *delim) ; Parse the string s into tokens using delim as delimiter. Computer Science: A Structured Programming Approach Using C 24 int main() { const char str[80] = "This is - www.tutorialspoint.com - website"; const char s[2] = "-"; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while( token != NULL ) { printf( " %s\n", token ); token = strtok(NULL, s); } return(0); } After compiling and running the above program, following result will be produced: This is www.tutorialspoint.com website 25 Source Code to Find the Frequency of Characters int main() { char c[1000],ch; int i,count=0; printf("Enter a string: "); gets(c); printf("Enter a characeter to find frequency: "); scanf("%c",&ch); for(i=0;c[i]!='\0';++i) { if(ch==c[i]) ++count; } printf("Frequency of %c = %d", ch, count); return 0; }Output Enter a string: This website is awesome. Enter a frequency to find frequency: e Frequency of e = 4 Computer Science: A Structured Programming Approach Using C 26 Arrays of Strings When we discussed arrays of pointers in Chapter 10, we introduced the concept of a ragged array. Ragged arrays are very common with strings. Consider, for example, the need to store the days of the week in their textual format. We could create a two-dimensional array of seven days by ten characters, but this wastes space. Computer Science: A Structured Programming Approach Using C 27 main() { char name[6][10]={ “ akshay”, “parag”, “raman”, “srinivas”, ”gopal” , “rajesh” }; Computer Science: A Structured Programming Approach Using C 28 Computer Science: A Structured Programming Approach Using C 29 Computer Science: A Structured Programming Approach Using C 30 Computer Science: A Structured Programming Approach Using C 31 PROGRAM 11-10 Print Days of the Week Computer Science: A Structured Programming Approach Using C 32 PROGRAM 11-10 Print Days of the Week Computer Science: A Structured Programming Approach Using C 33 FIGURE 11-13 Pointers to Strings Computer Science: A Structured Programming Approach Using C 34.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages34 Page
-
File Size-