Saturday 31 August 2013

 Reading / Writing Strings | C Programming Material pdf



Reading / Writing Strings

=> We use scanf ( ) function to read strings which do not have any white spaces.
Ex: scanf(“ %s ”, city );
=> When this statement is executed, the user has to enter the city name (eg “NEWDELHI”) without any white space (i.e not like “NEW DELHI”).
=> The white space in the string will terminate the reading and only “NEW” is assigned to city.
=> To read a string with white spaces gets( ) function or a loop can be used as.
(i) gets(city);
(ii) do … while loop
i = 0;
do
{
Ch = getchar( );
city[ i ] = ch;
i++;
}
while(ch ! = '\n') ;
i - - ; city[ i ] = '\0';
=> The copy or assign a single character to a string variable, the assignment can be written as given below;
city[ i ] = 'N';
=> When a string contains more than one character, the assignment is written as strcpy (city, “NEW DELHI”);
atoi( ) Function:
atoi( ) function is a C library function which is used to convert a string of digits to the integer value.
char st[10] = “24175” ;
int n;
n = atoi(st);
This will assign the integer value 24175 to the integer variable n.

(1) Write a C program to count the occurrence of a particular character in the given string.
Solution:
Consider a string “MISSISSIPPI”. Count the appearance of a character, say „S?.
Program:
\* program to count a character in a string * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[20], ch;
int count, l,i;
clrscr( );
printf(“ \n Enter the string:”);
gets(st);
printf(“ \n which char to be counted ?”);
scanf(“ %c”, &ch);
/* loop to check the occurrence of the character * /
l = strlen(st);
count = 0;
for( i=0; i < l; i++)
if(st[ i ] = = ch)
count ++;
printf(“ \n the character %c occurs %d times”, ch, count);
getch( ) ;
}
When this program is executed, the user has to enter the string and the character to be counted in the given string.
Output:
Enter the string : MISSISSIPPI
Which char to be counted? S
The character S occurs 4 times.

(2) Write a C program to count the number of vowels present in a sentence.
Solution
Consider a sentence “this is a book”. Count the appearance of vowels AEIOU in capital or small letters.
Program
/* program to count vowels * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[80], ch;
int count = 0, i;
clrscr( );
/* loop to read a string * /
printf(“ \n Enter the sentence: \n”);
gets(st);
/* loop to count the vowels in the string * /
for( i=0; i<strlen(st); i++)
switch(st [i ])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count ++;
break;
}
printf(“\n %d vowels are present in the sentence”, count);
getch( );
}
=> When this program is executed, the user has to enter the sentence.
=> Note that gets( ) function is used to read the sentence because the string has white spaces between the words.
=> The vowels are counted using a switch statement in a loop.
=> Note that a count++ statement is given only once to execute it for the cases in the switch statement.
Output:
Enter the sentence:
This is a book
5 vowels are present in the sentence.

(3) Write a C program to test whether a given string is palindrome string. explain the working of program.
Solution: Consider the string „LIRIL” when it is reversed, it reads again as “LIRIL”.
Any string of this kind is called a palindrome string.
? Few other palindrome strings are
DAD, MOM, MADAM, MALAYALAM
/* program to check for palindrome stirng */
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[20], rst[20];
int i,j;
clrscr( );
printf(„\n Enter the string:”);
scanf(“%s”, st);
/* loop to reverse the string */
i=0;
j=strlen(st)-1;
while( j >= 0 )
{
rst[ i ] = st[ j ];
i++;
j--;
}
rst[ i ] = „\0?;
if(strcmp(st,rst)==0)
printf(“\n %s is a palindrome string”, st);
else
printf(“\n %s is not a palindrome string”, st);
getch( );
}
=> When this program is executed, the user has to read a string.
=> To reverse this string, the original string is copied from the last position & written as
=> The two strings are compared using a strcmp ( ) function which will return a 0 when the original string and the reversed string are identical.
=> Based on this value the result is printed.
Output:
Enter the string : HYDERABAD
Hyderabad is not a palindrome string.

Alternative Method:
=> This program can also be written to compare the first character of the string with the last, second with the second last and so on up to the middle of the string as shown.
=> If all the characters are identical, then the string is a palindrome string.
=> Otherwise the loop will be terminated.

(4) Write a C program to compare two strings which are given as input through keyboard and print the alphabetically greater string.
Solution
The alphabetically greater string is the one whose ASCII value of the first letter is greater than that of the second string.
Consider the two character strings.
St1 = “ALPHA”
St2 = “BEETA”
St2 is greater than st1 because the ASCII value of 'B' in “BETA” is greater than that of 'A' in 'ALPHA'.
? Note that when the first letters of the two strings are identical, the second
letters of the strings are compared.
/* PROGRAM TO PRINT ALPHABETICALLY GREATER STRING * /
# include<stdio.h>
# include<conio.h>
# include<stirng.h>
main( )
{
char st1[20], st2[20];
clrscr( );
printf(“ \n Enter string 1:”);
scanf( “ %s ”,st1);
printf(“ \n Enter string 2:”);
scanf( “ %s ”, st2);
if(strcmp(st1,st2)> 0)
printf(“\n %s is alphabetically greater string”, st1);
else
printf(“ \n %s is alphabetically greater string”, st2);
getch( );
}
=> When this program is executed, the user has to enter the two strings.
=> Note that strcmp( ) function returns a positive value when string 1 is greater & a negative value when string 2 is greater.
Output:
Enter string 1 : ACPHA
Enter string 2 : BETA
BETA is alphabetically greater string.

(5) Write a C program to read an array of names and to sort them in alphabetical order (dictionary).
Solution:
Consider a list of names stored in a 2-Dimensional character array. Each row can store a name of a maximum length of 10 letters.
=> Compare the name in the first row with the Second, then with the third name and so on… till the smallest name ( & name starting with letter „A?) moves to
the first place.
=> Now compose the second name with the third then with the fourth name & so on.. till the alphabetically second smallest name none to the second place.
Names
This procedure is repeated till the names are arranges in alphabetical order shown below.
Step involved in arranging names in alphabetical order are given below.
Step 1 : Read n
Step 2 : Loop to read „n? names of the list
Step 3 : Loop to arrange the names by comparison
Step 4: Loop to print the arranged list.
Step 5 : Stop
PROGRAM TO ARRANGE NAMES IN ALPHABETICAL ORDER.
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char names[50][20], temp[20];
int n,i,j;
clrscr( ):
printf(“ \n How many names?”);
scanf(“%d”, &n);
printf(“ \n Enter the %d names one by one \ n”,n);
for( i=0; i<n; i++)
scanf(“%s”, names[ i ]);
/* loop to arrange names in alphabetical order * /
for( i=0; i<n-1; i++)
for ( j =i+1; j<n; j++)
if(strcmp(names[ i ], names[ j ]) > 0)
{
strcpy( temp, names[ i ]);
strcpy(names[ i ], names[ j ]);
strcpy(names[ j ],temp);
/* loop to print the alphabetical list of names * /
printf (“ \n names in alphabetical order”);
for( i=0; i<n; i++)
printf(“\n %s”, names[ i ]);
getch( );
}
=> When this program is executed, the user has to first enter the total no of names (n) and then all the names in the list.
=> The names are compared, rearranged and printed in alphabetical order.

Output:
How many names? 4
Enter the 4 names one by one
DEEPAK
SHERIN
SONIKA
ARUN
Names in Alphabetical order
ARUN
DEEPAK
SHERIN
SONIKA

(6) Write a C program to convert a line in the lower case text to upper case.
Solution
Consider ASCII values of lower and upper case letters
A – 65 B – 66 …………………… Z – 90
a - 97 b – 98 …………………… z – 122
* Note that the difference between the lower and upper case letters (ie. – 32) is used for conversion.
/* PROGRAM TO CONVERT LOWER CASE TEST TO UPPER CASE * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[80];
int i;
clrscr( );
printf(“ \ n Enter a sentence : \ n”);
gets(st);
/* loop to convert lower case alphabet to upper case * /
for( i=0; i<strlen(st); i++)
if(st[ i ] >= „a? && st[ i ] <= „z?)
st[ i ] = st[ i ] – 32;
printf(“ \n the converted upper case strings \n %s”, st);
getch( );
}

OUTPUT:
Enter a sentence:
Logical thinking is a must to learn programming
The converted upper case string is
LOGICAL THINKING IS A MUST TO LEARN PROGRAMING.

(7) Write a C program to count no of lines, words and characters in a given text.
Solution:
A while loop is used to read the text. The character '$' is used to terminate the reading of text.
Program:
# include<stdio.h>
# include<string.h>
# include<conio.h>
main( )
{
char txt[250], ch, st[30];
int ins, wds, chs, i;
clrscr( ) ;
printf(“ \n Enter the text, type $ st end \n \n”);
i=0;
while((txt[i++]= getchar( ) ) ! = '$');
i--;
st[ i ] = '\0' ;
ins = wds = chs = 0;
/ * loop to count lines, words & characters in text * /
i=0;
while(txt[ i ]!=?$?)
{
switch(txt[ i ])
{
case ',':
case '!':
case '\t':
case ' ':
{
wds ++;
chs ++;
break;
}
case '?':
case '.':
{
wds ++;
chs ++;
break;
}
default: chs ++;
break;
}
i++;
}
printf(“\n\n no of char (incl.blanks) = %d”, chs);
printf(“\n No. of words = %d”, wds);
printf(“\n No of lines = %d”, lns);
getch( ) ;
}

Output:
Enter the text, type $ at end
What is a string? How do you initialize it? Explain with example.
With example. $
No of char (inch. Blanks) = 63
No of words = 12
No of lines = 3

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved