Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory
CS19001: Programming and Data Structures Laboratory
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
02-09-14
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Table of Contents
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings
1
Tutorial: Strings
2
Programming Assignments
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Table of Contents
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings
1
Tutorial: Strings
2
Programming Assignments
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Strings In C, a string is defined to be a null-terminated character array. The null character ’\0’ is used to indicate the end of the string. int main () { char greet [3]={ ’H ’ , ’i ’ , ’ \0 ’ }; printf ( " Greeting message : % s \ n " , greet ); return 0; }
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
Variation in initialization char char char char
c []= " abcd " ; c [5]= " abcd " ; c []={ ’a ’ , ’b ’ , ’c ’ , ’d ’ , ’ \0 ’ }; c [5]={ ’a ’ , ’b ’ , ’c ’ , ’d ’ , ’ \0 ’ }; Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory
Reading 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; }
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
Enter name: Dennis Ritchie Your name is Dennis. scanf() function takes only string before the white space.
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory
Reading a line of text int main (){ char name [30] , ch ; int i =0; printf ( " Enter name : " ); while ( ch != ’\ n ’) { // t e r m i n a t e s if user hit enter ch = getchar (); name [ i ]= ch ; i ++; } // i n s e r t i n g null c h a r a c t e r at end name [ i ]= ’ \0 ’; printf ( " Name : % s " , name ); return 0; } Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory
Better method int main (){ char name [30]; printf ( " Enter name : " ); gets ( name ); // Function to read string from user . printf ( " Name : " ); puts ( name ); // Function to display string . return 0; }
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
Enter name: Dennis Ritchie Name: Dennis Ritchie
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory
Passing Strings to Functions void Display ( char ch []); int main (){ char c [50]; printf ( " Enter string : " ); gets ( c ); Display ( c ); // Passing string c to function . return 0; } void Display ( char ch []){ printf ( " String Output : " ); puts ( ch ); }
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Library functions # include < stdio .h > # include < string .h > int main () { char str1 [12] = " Hello " ; char str2 [12] = " World " ; char str3 [12]; int len ; strcpy ( str3 , str1 ); printf ( " strcpy ( str3 , str1 ): % s \ n " , str3 ); strcat ( str1 , str2 ); printf ( " strcat ( str1 , str2 ): % s \ n " , str1 ); len = strlen ( str1 ); printf ( " strlen ( str1 ) : % d \ n " , len ); return 0; } Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Result
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings
strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10
Programming Assignments
do not forget to include string.h
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
A bit more about string manipulation
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
int strcmp (char s[], char t[]): Returns 0 if the two strings are identical, a negative value if s is lexicographically smaller than t (i.e., if s comes before t in the standard dictionary order), and a positive value if s is lexicographically larger than t. Comparison is done with respect to ASCII values (A - 65, a - 95)
Tutorial: Strings Programming Assignments
int strlen (char s[]): Returns the length (the number of characters before the first null character) of the string s.
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Table of Contents
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings
1
Tutorial: Strings
2
Programming Assignments
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Assignment 1: Complete and submit during lab Write a C program which reads three character strings str 1, str 2, and str 3 of size 20, 4 and 5 respectively and display the strings. 1
The program checks whether str 2 occurs in str 1 as a substring and in that case displays the starting index of all such occurrences.
2
The program replaces all occurrences of str 2 in str 1 by str 3 and displays the newly formed string. If that is not possible due to space limitation, then an appropriate message is printed.
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
[6]
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Assignment 2: Complete and submit during lab Write a C program which 1
declares two 1-d character arrays word[max size] and sentence[MAX SIZE], initialize integer variable ‘count’ to 0
2
asks the user to enter a string without intermediate white space (w say) and stores in word[max size]
3
asks the user to enter a sentence and stores in sentence[] (no. of char < MAX SIZE)
4
counts the frequency of w in sentence and adds up to ’count’. If the entered sentence is blank(\n is first character), then break. Else, goto step 3.
5
report the value of count.
Test run is given in next slide Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
[6] CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Assignment 2: Complete and submit during lab
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Enter word: algebra Enter sentence 1: algebra is boring (enter pressed) Enter sentence 2: Boolean algebra is part of syllabus (enter pressed) Enter sentence 3: (enter pressed) Frequency = 2
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Tutorial: Strings Programming Assignments
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Assignment 3: Complete and submit during lab Consider a string s1 = “abcadefbas” and a string s2 = “abc”. A prefix of s1 is any substring which starts from the first character of s1 . For example, “abc”, “ab”, “a”, “abcad” · · · are all prefixes of s1 . We are interested in finding the maximum length possible for a prefix of s1 such that all characters in the prefix are present in s2 . In our example with s1 and s2 , the maximum length is 4 for the prefix “abca” of s1 (the next character ’d’ is not present in s2 ). Write a C function
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
int max prefix(char s1[ ], char s2[ ]) where s1 and s2 contain null-terminated strings. The function returns the maximum length possible of a prefix of s1 with respect to s2 as defined above. [10] Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments
Assignment 4: Complete and submit during lab Write a C function void balance(int A[ ], int n, int k) which takes as parameter an array A with n elements and does the following: 1
Finds out whether the maximum of A[0] . . . A[k − 1] is greater than the minimum of A[k] . . . A[n − 1].
2
In case it is so, the function swaps the maxima and the minima and goes back to step (a).
3
In case it is not so, the function returns.
CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings Programming Assignments
Example run: Consider A = {12, 10, 20, 5, 6, 15} and the function call balance(A, 6, 3). A correct execution of balance() should ensure that A = {6, 10, 5, 20, 12, 15} after the function returns. [8] Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
CS19001: Programming and Data Structures Laboratory
Tutorial: Strings Programming Assignments CS19001: Programming and Data Structures Laboratory Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur Tutorial: Strings
Thank You
Soumyajit Dey, Rajib Mall CSE, IIT Kharagpur
Programming Assignments
CS19001: Programming and Data Structures Laboratory