strings. sentences in english are implemented as strings in the c language. computations involving...
Post on 23-Dec-2015
225 Views
Preview:
TRANSCRIPT
Strings
Strings
• Sentences in English are implemented as strings in the C language.
• Computations involving strings are very common.
• E.g.
– Is string_1 the same as string_2 ?
– Append string_1 at the end of string_2
– Sort a collection of strings in ascending order
– Find occurrence of a character in a given sentence (string)
Strings
• The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0‘.
• The following declaration and initialization create a string consisting of the word "Hello“:
• char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Strings
• This is how this char array looks in memory contains the above string
• Be careful about the length of string’s array length
• No bounds checking by the compiler
• May result in unpredictable results
H e l l o \0
Strings Example – Placement of NULL
• Write a program to declare a char array, initialize it to the string “Hello” and then move around the NULL character to see its effects
Strings Manipulation
• Because dealing with strings is cumbersome, a library of functions is available for programmers to manipulate strings
• Some of the more frequently used string manipulation functions are:– strlen(str); //returns length of a str– strcpy(dest_str, src_str);//copies source str in dest str – strcat(dest_str, src_str); //append source str at the end of dest str– strcmp(Str1, Str2);// compares two strings lexicographically
//returns 0 if both are equal, negative number if LHS is < RHS
Strings Example – strlen() function
• Write a program to declare a char array, initialize it to a string and then traverse the string in forward and backward direction
Strings Example – strcpy(), strcat() functions
• Write a program to declare two char arrays and initialize them to some strings. Declare a third char array and copy a concatenation of the first two strings into the third array.
String I/O
• Another convenient way for string I/O is:
– gets(str); //read a string from user and store in str– puts(str); //print a string contained in str
Strings Example – strcmp() function
• Write a program to declare two char arrays and initialize them to some strings. Compare the two strings using strcmp() function
Strings Example – strcmp() function
• Write a program to input a list of names. Sort the list of names in ascending (lexicographical / dictionary) and descending orders.
Algorithm – Sorting an array of names (strings)
• You have a 2-Dimensional array containing 4 names
char names[4][50];
The array has 4 rows and 50 columns
each row can hold one name of length 50 characters (including the NULL that comes at the end of a string
Algorithm – Sorting an array of names (strings)
• To input names, you only need to specify the row where the name is to be stored
for(i=0;i<10;++i) gets(str[i]);
Here, i is the index of the row where every name will be stored
The function gets() takes care of where every character of a name goes and also the string terminating NULL character
Algorithm – Sorting an array of names (strings)
for(i=0;i<10;++i) gets(str[i]);
After the for loop finishes execution the array of names will look like this:names =
Algorithm – Sorting an array of names (strings)
Loops to compare strings and swap places
for(i=0;i<3;++i) for(j=i+1;j<4 ;++j){ if(strcmp(str[i],str[j])> 0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } }
Algorithm – Sorting an array of names (strings)
1. start from row # 1 (variable i)2. Compare row 1 with rows 2, 3 and 4 (up to i-1), one row at a time3. Smaller of the two names being compared must occupy the upper rowRow 1
Row 2
Row 3
Row 4
Col 1, col 2, col 3, …. Col 50
Algorithm – Sorting an array of names (strings)
ij
Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
The array now looks like this
a d a m \0 …t y l e r \0 …s o p h i a \0 …r a c h e l \0 …
ij
Algorithm – Sorting an array of names (strings)
a d a m \0 …t y l e r \0 …s o p h i a \0 …r a c h e l \0 …
i
j
Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …t y l e r \0 …s o p h i a \0 …r a c h e l \0 …
i
j
Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …t y l e r \0 …s o p h i a \0 …r a c h e l \0 …
i
j
i is incremented (i++), j is re-initialized to j=i+1Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …s o p h i a \0 …t y l e r \0 …r a c h e l \0 …
i
j
Array now looks like this
Algorithm – Sorting an array of names (strings)
a d a m \0 …s o p h i a \0 …t y l e r \0 …r a c h e l \0 …
i
j
Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …r a c h e l \0 …t y l e r \0 …s o p h i a \0 …
i
j
Array now looks like this
Algorithm – Sorting an array of names (strings)
a d a m \0 …r a c h e l \0 …t y l e r \0 …s o p h i a \0 …
i
j
i is incremented (i++), j is re-initialized to j=i+1Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …r a c h e l \0 …s o p h i a \0 …t y l e r \0 …
i
j
Compare and swap if(strcmp(str[i],str[j])> 0)
Algorithm – Sorting an array of names (strings)
a d a m \0 …r a c h e l \0 …s o p h i a \0 …t y l e r \0 …
Finally, the sorted array looks like this
Strings Example – counting words
• Write a program to input a sentence (string) and count the number of words in it.
Strings Example – frequency of a character
• Write a program to input a sentence (string) and find the frequency of a given character.
Strings Example – Palindrome or Not
• Write a program to input a word and find out if it is a palindrome.
• Civic, mom, dad, kayak, radar, madam, racecar
• Function strrev(str) reverses a string
top related