Programming in C: Unit II (b): Strings

Arrays of Strings

with Example C Programs

Till now we have seen that a string is an array of characters. For example, if we say char name[] "Mohan", then name is a string (character array) that has five characters.

ARRAYS OF STRINGS

Till now we have seen that a string is an array of characters. For example, if we say char name[] "Mohan", then name is a string (character array) that has five characters. Now suppose that there are 20 students in a class and we need a string that stores names of all the 20 students. How can this be done? Here, we need a string of strings or an array of strings. Such an array of strings would store 20 individual strings. An array of string is declared as,

char names [20] [30];

Here, the first index will specify how many strings are needed and the second index specifies the length of every individual string. So here, we allocate space for 20 names where each name can be a maximum of 30 characters long. Hence, the general syntax for declaring a two-dimensional array of strings can be given

<data type> <array_name> [row_size] [column_ size];

Let us see the memory representation of an array of strings. If we have an array declared as

char name [5] [10] = {"Ram", "Mohan", "Shyam", "Hari", "Gopal"};

Then in memory the array is stored as shown in Figure 6.20.

By declaring the array names, we allocate 50 bytes. But the actual memory occupied is 27 bytes. Thus we see, more than half of the memory allocated lies wasted. Figure 6.21 shows an algorithm to process an individual string from an array of strings.

Programming Tip: When accessing elements of a character array, make sure that the elements are within the array boundaries.

In Step 1, we initialize the index variable I to zero. In Step 2, a while loop is executed until all the strings in the array are accessed. In Step 3, each individual string is processed.

16. Write a program to read and print the names of n students of a class.

#include <stdio.h>

#include <conio.h>

int main()

{

char names [5] [10];

int i, n;

clrscr();

printf("\n Enter the number of students: ");

scanf("%d", &n);

for (i=0;i<n;i++)

{

printf("\n Enter the name of student %d: ", i+1);

gets (names [i]);

}

printf("\n Names of the students are:\n");

for (i=0; i < n; i++)

puts (names[i]);

getch();

return 0;

}

Output

Enter the number of students: 3

Enter the name of student 1: Aditya

Enter the name of student 2: Goransh

Enter the name of student 3: Sarthak

Names of the students are: Aditya Goransh Sarthak

17. Write a program to sort names of students.

#include <stdio.h>

#include <conio.h>

int main()

{

char names [5] [10], temp [10];

int i, n, j;

clrscr();

printf("\n Enter the number of students: ");

scanf("%d", &n);

for (i=0;i<n;i++)

{

printf("\n Enter the name of the student %d: ", i+1);

gets (names [i]);

}

for (i=0;i<n;i++)

{

for(j=0;j< n-i-1;j++)

{

if (strcmp (names [j], names [j+1]) >0)

{

strcpy(temp, names [j]);

strcpy (names [j], names [j+1]);

strcpy (names [j+1], temp);

}

}

}

printf("\n Names of the students are: ");

for (i=0;i<n;i++)

puts (names [i]);

getch();

return 0;

}

Output

Enter the number of students: 3

Enter the name of student 1: Sarthak

Enter the name of student 2: Goransh

Enter the name of student 3: Aditya

Names of the students are: Aditya Goransh Sarthak

18. Write a program to read and print the text until a * is encountered. Also count the number of characters in the text entered.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[100];

int i=0;

clrscr();

printf("\n Enter * to end");

printf("\n Enter the text: ");

scanf("%c", &str[i]);

while (str[i] != ‘*’)

{

i++;

scanf("%c", &str[i]);

}

str[i] = '\0';

printf("\n The text is: ");

i=0;

while (str[i] != '\0')

{

printf("%c", str[i]);

i++;

}

printf("\n The count of characters is: %d", i);

return 0;

}

Output

Enter * to end

Enter the text: Hi there*

The text is: Hi there

The count of characters is: 8

19. Write a program to read a sentence. Then count the number of words in the sentence.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[200];

int i=0, count=0;

clrscr();

printf("\n Enter the sentence: ");

gets (str);

while (str[i] != '\0')

{

if (str[i] == && str[i+1] != ` ')

count++;

i++;

}

printf("\n The total count of words is: %d", count+1);

return 0;

}

Output

Enter the sentence: How are you

The total count of words is: 3

 

20. Write a program to read multiple lines of text until a* is entered. Then count the number of characters,words, and lines in the text.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[200];

int i=0, word_count = 0, line_count =0, char_count = 0;

clrscr();

printf("\n Enter a * to end");

printf("\n Enter the text: ");

scanf("%c", &str[i]);

while (str[i] != '*')

{

i++;

scanf("%c", &str[i]);

}

str[i] = '\0';

i=0;

while (str[i] != '\0')

{

if (str[i] =='\n' || i==79)

line_count++;

if (str[i] = '' && str[i+1] !=` ')

word_count++;

char_count++;

i++;

}

printf("\n The total count of words is: %d", word_count+1);

printf("\n The total count of lines is: %d", line_count+1);

printf("\n The total count of characters is: %d", char_count);

return 0;

}

Output

Enter the text: Hi there*

The total count of words is: 2

The total count of lines is: 1

The total count of characters is: 8

21. Write a program to copy n characters of a string from the mth position in another string.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[1000], copy_str [1000];

int i=0, j=0, m, n;

clrscr();

printf("\n Enter the text: ");

gets (str);

printf("\n Enter the position from which to start: ");

scanf("%d", &m);

printf("\n Enter the number of characters to be copied: ");

scanf("%d", &n);

i = m;

while (str[i] != '\0' && n>0)

{

copy_str[j] = str[i];

i++;

j++;

n--;

}

copy_str[j] = '\0';

printf("\n The copied text is: ");

puts (copy_str);

return 0;

}

Output

Enter the text: How are you?

Enter the position from which to start: 2

Enter the number of characters to be copied: 5

The copied text is: w are

22. Write a program to enter a text that has commas. Replace all the commas with semi colons and then display the text.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[1000], copy_str [1000];

int i=0;

clrscr();

printf("\n Enter the text: ");

gets (str);

while (str[i] != `\0')

{

if (str[i] ==',')

copy_str[i]=' ; `;

else

copy_str[i] = str[i];

i++;

}

copy_str[i] = '\0';

printf("\n The copied text is: ");

i=0;

while (copy_str[i] != '\0')

{

printf("%c", copy_str[i]);

i++;

}

return 0;

}

Output

Enter the text: Hello, How are you

The copied text is: Hello; How are you

23. Write a program to enter a text that contains multiple lines. Rewrite this text by printing line numbers before the text of the line starts.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[1000];

int i=0, linecount = 1;

clrscr();

printf("\n Enter a * to end");

printf("\n ********");

printf("\n Enter the text: ");

scanf("%c", &str[i]);

while (str[i] != '*')

{

i++;

scanf("%c", &str[i]);

}

str[i] =  '\0';

i=0;

while (str[i] != '\0')

{

if (linecount == 1 && i == 0)

printf("\n %d\t", linecount);

if (str[i] == `\n`)

{

linecount++;

printf("\n %d\t", linecount);

}

printf("%c", str[i]);

i++;

}

Return o;

}

Output

Enter a * to end

**************

Enter the text:

Hello

how

are You?*

1 Hello

2 how

3 are you?

24. Write a program to enter a text that contains multiple lines. Display the n lines of text starting from the mth line.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[1000];

int i = 0, m, n, linecount = 0;

clrscr();

printf("\n Enter a * to end ");

printf("\n Enter the text: ");

scanf("%c", &str[i]);

while (str[i]!='*')

{

 i++;

scanf("%c", &str[i]);

}

str[i] = '\0';

printf("\n Enter the line number from which to copy: ");

scanf("%d", &m);

printf("\n Enter the line number till which to copy: ");

scanf("%d", &n);

i=0,

while (str[i] != '\0')

{

if (linecount == m)

{

j = i;

while (n>0)

{

printf("%c", str[j]);)

j++;

if (str[j] =='\n')

{

n--;

linecount++;

printf("%d \t", linecount);

}

}

}

else

{

i++;

if (str[i]=='\n')

linecount++;

}

}

getch();

return 0;

}

Output

Enter a * to end

Enter the text: Hello

how

are you?

*

Enter the line number from which to copy: 1

Enter the line number till which to copy: 2

Hello 1

how 2

25. Write a program to enter a text. Then enter a pattern and count the number of times the pattern is repeated in the text.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[200], pat [20];

int i=0, j=0, found=0, k, count=0;

clrscr();

printf("\n Enter the string: ");

gets (str);

printf("\n Enter the pattern: ");

gets (pat);

while (str[i]!= '\0')

{

j=0, k=i;

while (str [k] ==pat [j] && pat [j] != '\0')

{

k++;

 j++;

}

if (pat [j]=='\0')

{

found=1;

count++;

}

i++;

}

if (found==1)

printf("\n PATTERN FOUND %d TIMES", count);

else

printf("\n PATTERN NOT FOUND");

return 0;

}

Output

Enter the string: She sells sea shells on the sea shore

Enter the pattern: sea

PATTERN FOUND 2 TIMES

26. Write a program to find whether a given string is a palindrome or not.

#include <stdio.h>

#include <conio.h>

int main()

{

char str[100];

int i = 0, j, length = 0;

clrscr();

printf("\n Enter the string: ");

gets (str);

while (str[i] != '\0')

length++;

i++;

i=0;

j = length - 1;

while (i <= length/2)

{

if (str[i] == str[j])

{

i++;

j--;

}

else

break;

}

if (i>=j)

printf("\n PALINDROME");

else

printf("\n NOT A PALINDROME");

return 0;

}

Output

Enter the string: madam

PALINDROME

27. Write a program to implement a quiz program.

#include <stdio.h>

#include <string.h>

#include <conio.h>

main()

{

char quest [5] [100];

char option1 [3] [20], option2 [3] [20], option3 [3] [20], option4 [3] [20],

option5 [3] [20];

int response [5], correct_ans [5], option, i, marks;

clrscr();

strcpy (quest [0], "Name the capital of India");

strcpy (option1 [0], "1. Mumbai");

strcpy (option1 [1], "2. New Delhi");

strcpy (option1 [2],"3. Chennai");

correct_ans [0] = 1;

boys stropy (quest [1], "Name the national bird of India");

strcpy (option2 [0], "1. Peacock");

strcpy (option2 [1], "2. Sparrow");

strcpy (option2 [2],"3. Parrot");

correct_ans [1]=0;

strcpy (quest [2],"Name the first prime minister of India");

strcpy (option3 [0], "1. M D Gandhi") ;

strcpy (option3 [1], "2. S D Sharma");

strcpy (option3 [2], "3. J L Nehru");

correct_ans [2]=2;

strcpy (quest [3], "Name the first female president of India");

strcpy (option4 [0],"1. Pratibha Patil");

strcpy (option4 [1], "2. Sonia Gandhi");

strcpy (option4 [2],"3. Indira Gandhi");

correct_ans [3] = 0;

strcpy (quest [4], "Name the youngest prime minister of India");

strcpy (option5 [0], "1. Rajiv Gandhi");

strcpy (option5 [1], "2. Sanjay Gandhi");

strcpy (option5 [2],"3. Rahul Gandhi");

correct_ans [4] = 0;

do

{

printf("\n\n\n\n QUIZ PROGRAM");

printf("\n******************");

printf("\n 1. Display Questions");

printf("\n 2. Display Correct Answers");

printf("\n 3. Display Result");

printf("\n 4. EXIT");

printf("\n ******************");

printf("\n\n\n Enter your option: ");

scanf("%d", &option);

switch (option)

{

case 1:

printf("\n %s\n", quest [0]);

for(i=0;i<3;i++)

printf("\n %s", option1 [i]);

printf("\n\n Enter your answer number: ");

scanf("%d", &response [0]);

printf("\n %s\n", quest [1]);

for (i=0;i<3;i++)

printf("\n %s", option2 [i]);

printf("\n\n Enter your answer number: ");                         

scanf("%d", &response [1]);

printf("\n %s\n", quest [2]);

for (i=0;i<3;i++)

printf("\n %s", option3 [i]);

printf("\n\n Enter your answer number: ");

scanf("%d", &response [2]);

printf("\n %s\n", quest [3]);

for(i=0;i<3;i++)

printf("\n %s", option4 [i]);

printf("\n\n Enter your answer number: ");

scanf("%d", &response [3]);

printf("\n %s\n", quest [4]);

for (i=0;i<3;i++)

printf("\n %s", option5 [i]);

printf("\n\n Enter your answer number: ");

scanf("%d", &response [4]);

break;

case 2:

printf("\n\n CHECK THE CORRECT ANSWERS");

printf("\n *******************") ;

printf("\n %s \n %s", quest [0], option1 [correct_ans [0]]);

printf("\n\n %s \n %s", quest [1], option2 [correct_ans [1]]);

printf("\n\n %s \n %s", quest [2], option3 [correct_ans [2]]);

printf("\n\n %s \n %s", quest [3], option4 [correct_ans [3]]);

printf("\n\n %s \n %s", quest [4], option5 [correct_ans [4]]);

break;

case 3:

marks = 0;

for (i=0;i<= 4;i++)

{

if (correct_ans [i] +1== response [i])

marks++;

}

printf("\n Out of 5 you score %d",marks);

break;

}

while (option!=4);

getch();

return 0;

}

Programming in C: Unit II (b): Strings : Tag: : with Example C Programs - Arrays of Strings