Arrays 203
char f;
clrscrO;
puts ("Enter a String:");
gets (s);
puts("Enter a Character to Find
f=getchar();
for (i=0,H<=15,n++)
I
if(s[i]~f)
C + + ;
I
printf ("The Character (%c) in a String (%s) occurs %d times ”,f,s,c);
I
PITIFUL
Enter a String: programmer
Enter a Character to Find: r
The Character (r) in a String (programmer) occurs (3) times.
Explanation In this program the string and a single character is entered through the keyboard.
Inside the for loop, i f statement checks each element of the string for occurrence of the single entered
character. If the character ( ' r ') is found then c counter is increased otherwise without increasing the
counter the loop continues till' i ' reaches to 15. At last the value of c gives the total occurrence of
given character.
7.7 Write a program to display the elements of two arrays in two separate columns and add
their corresponding elements. Display the result of addition in the 3rd column.
mainO
{
in t i,num[]={24,34,12,44,56,17};
int numl[]={12,24,35,78,85,22};
c lrs c rO ;
printf ("Element of Array 1st- 2nd Array Addtion
");
for (i=0,n<=Sd++)
printf ("nt %d + %d = %d",num[i],numl[i],num[i]+numl[i]);
I
O U TPUT;
Element of Array 1st - 2nd Array Addition
24 + 12 = 36
34 + 24 = 58
12 + 35 = 47
44 + 78 = 122
56 + 85 =141
17 + 22 = 39