Working with Strings & Standard Functions 271
OUTPUT:
Enter String : IND IA IS GREAT
Enter Character: G
String from given Character : GREAT
Explanation In the above program two strings and character pointer are declared. The string^ are
entered. Both the strings are used as arguments with function s t r p b r k ( ) . This function finds first
occurrence of second string in the first string and returns that address which is assigned to character
pointer * p t r . The pointer p t r displays the rest string.
Here, the first string is "IN D IA IS GREAT" and second string is "G ." The ' G' occurs at the beginning
of the third word. Hence, on execution of the program the string from ' G ' onwards is displayed. The
output is only "GREAT. "
8.5 APPLICATIONS OF STRINGS
8.35 Write a program to count a character that appears in a given text for number of times. Use the
while loop.
# include <stdio.h>
# include <conio.h>
void main ()
{
char text [20] ;
char find;
int i=0, ccaint=0;
clrscr();
printf ("T ype Text B elow A n");
gets(text);
printf ("T ype a character to count
find=getche();
while (te x t[i]!= ' 0 r)
I
if(text[i]~ fin d )
count++;
i++;
I
printf C'nCharacter(%c) found in Given String = % d Times "find^ount);
I
OUTPUT:
Type Text Below.
Programming
Type a character to count: m
Character (m) found in Given String = 2 Times.
272 Programming and.D ata Structures
Explanation In the above program a string is entered. A single character, which is to be searched in
the string, is also entered. The i f condition in the while loop checks every character of the string with
the single character. If there is a match counter variable ' count7 gets increased. After the complete
execution of w hile loop the counter displays number of times the characters found in the string.
8.36 Write a program to count'm' characters that appears in a given string without using
any function. Use the for loop.
# include <stdio.h>
# include <conio.hr*
void main ()
{
char text [25];
int i ,count=0;
clrscrO ;
for (i=0,H<25;++i)
{
scanf ("% c " A textli ]);
printf ("% c ",text[i]);
if (t e x t [i ]~ ' n ')
break;
else
if (t e x t [i]~ 'm ')
++count;
i
printf ("Character'm 'Found in Text=%d timesn'count);
getcheO;
Q M rU T ;
Programming is a skill.
Character'm' Found in Text=2 times.
Explanation The logic of the program is same as that of the previous one. Here, in this program the
character that is to be searched is 'm' which is a default character. The first i f statement within the
f o r loop terminates the loop when the user presses " e n t e r " key. The second i f statement checks
every character of the entered string with %m/. If there is a match counter variable ' count' is increased.
Thus, at last count variable gives the number of times
xm'
is present in the string.
8.37 Write a program to count the following characters that appears in a string without using
any functions.
1. 'm'
2. V
3. 'o'
# include <stdio.h>
# include <conio.h>
void main ()
{
Working with Strings & Standard Functions 273
char text [25] ="Prograinning is good habit";
int i,mB0,O30,r»0;
clrscr();
fo r (i=0,-i<25;++i)
I
if(tex t[i]= = 'm ')
++m;
if (te x t[i]= = Y )
++r;
if (t e x t [i ]~ 'o ')
+ + o ;
I
printf ("nCharacter'm 'Found in Text=%d times.n",m);
printf ("nCharacter Y F oun d in Text=%d times.n",r);
printf ("nCharacter 'o'Found in Text=%d times.n",o);
getcheO;
I
O UT P U T:
Character'm' Found in Text=2 times.
Character V Found in Text=2 times.
Character 'o' Found in Text=3 times
Explanation The logic of the program is same as that of the previous one. Here, in this program the
characters that are to be searched are %m', ' r ' , and 'o '. The i f statements within the fo r loop
increases the respective counter variables when there is a match of these characters. Thus, after execution
of the fo r loop the three counter variables ' m',' r v and x o ' are printed.
8.38 Write a program to copy contents of one string to another string without using the
function.
# include <stdio.h>
# include <conio.h>
void main()
{
char ori [20] ,dup [20];
int i;
clrscr();
printf ("Enter Your Name
gets(ori);
for (i=0;ori[i]!='0',n++)
dup[i]=ori[i];
dup[i]='0';
printf ("Origional String: %s",ori);
printf ("nDuplicate String: %s",dup);
274 Programming and Data Structures
OUTPU T :
Enter Your Name: SACHIN
Origiortal String: SACHIN
Duplicate String: SACHIN
Explanation In the above program two character arrays are declared. The source string is entered.
Using the fo r loop and assignment operator each character of the source array (or i [ ] ) is assigned
to target array dup [ ] . After execution of the fo r loop, NULL character is appended in the target string
to mark the end of the string. Usingprint f () function both the strings are displayed.
8.39 Write a program to know whether the entered character string is palindrome or not.
(Palindrome word reads same from left to right & right to left.)
Ex. DAD, ABBA, MUM)
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <process. h>
void main ()
{
char str[1 0 ];
int i«0 ,j,te s t;
clrsc rO ;
printf ("Enter the word
scanf ("% s ”,str);
j=strlen(str)-l;
while (i< = j)
{
if(s tr [i]~ s t r [j])
test=l;
else
I
test=0;
break;
I
i
if (te s t ~ 1 )
printf ("W ord is palindrome.n");
else
printf (" n Word is notPalindrome.n");
}
OU TP U T;
Enter the word: ABBA
Word is palindrome.
Working with Strings & Standard Functions 275
Explanation In the above program string is entered that is to be tested for PALINDROME. The string
length is calculated and assigned to variable ' j ' . The value o f ' j ' is less by one with original string
length because the array elements are counted from zero (0).
The i f statement within the w h ile loop checks the first and last character of string for equality.
Counter variable ' i ' and ' j ' denote the first and the last characters respectively. To get the successive
characters from both the ends variable 1 i ' is increased and % j ' is decreased. Till the match variable
'test' is 1 and the loop continues otherwise test is set to zero (0) and the break statement terminates the
loop.
8.40 Write a program to compare the strings using strcmpO function and display their ASCII
difference. Initialize the strings and copy some names of the cities to the variables.
# include <canio.h>
# include <stdio.h>
# include <string.h>
mainQ
{
char a l [15], a2 [1 5 ], a3 [15], a4 [15 ], a5 [15], a6 [15 ];
in t c l, c2, c3 r
strcpy (al,"NAGPUR");
strcpy(a2,"NAGPOR*);
Strcpy(a3, "PANDHARPOR");
strcpy(a4,"KDIBAPDR");
Strcpy(a5,"AURANGABAD");
Strcpy(a6, "HYDERABAD");
clrscr();
c=strcnp(al,a2);
c2«stranp(a3,a4);
c3=strcmp(a5,a6);
printf ( nAscii Difference between two stringsn");
printf ("Difference between (% s % s)= % d n"^ ilfa2,cl);
printf ("Difference between (% s %s)=%dn",a3,a4j:2);
printf ("Difference between (% s % s)=% dn",a5,a6^3);
getcheO;
I
OUTPUT:
Difference between (NAGPUR NAGPUR)= 0
Difference between (PANDHARPUR KOLHAPUR)= 5
Difference between (AURANGABAD HYDERABAD)=-7
Explanation In the above program five character arrays are declared. Using strcpy () function
names of cities are copied to arrays. Using strcmp () function strings are compared. The strcnp 0
returns the ASCI I difference of two strings. The ASCII values of the first characters of two strings are
taken into account for comparison. Rest of the elements are not considered for ASCII difference. The
ASCII value of the first character of first string is subtracted from ASCII value of the first character of
a second string. The Table 8.1 given below illustrates the calculation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93