strings. sentences in english are implemented as strings in the c language. computations involving...

30
Strings

Upload: evangeline-bailey

Post on 23-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Strings

Page 2: 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

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)

Page 3: 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

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'};

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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.

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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.

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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 =

Page 15: 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

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); } }

Page 16: 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

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

Page 17: 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

Algorithm – Sorting an array of names (strings)

ij

Compare and swap if(strcmp(str[i],str[j])> 0)

Page 18: 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

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

Page 19: 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

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)

Page 20: 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

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)

Page 21: 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

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)

Page 22: 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

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

Page 23: 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

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)

Page 24: 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

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

Page 25: 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

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)

Page 26: 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

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)

Page 27: 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

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

Page 28: 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

Strings Example – counting words

• Write a program to input a sentence (string) and count the number of words in it.

Page 29: 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

Strings Example – frequency of a character

• Write a program to input a sentence (string) and find the frequency of a given character.

Page 30: 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

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