Write a program that completed following tasks on string.
Given string is “DBJ College ,Chiplun DBJ DBJ” .
1) find the occurrence of “D’ in given string.
2) Find the occurrences of “DBJ” in given string.
source file name: findchar.C
#include<stdio.h>
#include<string.h>
int main()
{
char s[] = "DBJ College, Chiplun. DBJ DBJ "; // String Given
char ch = 'D'; // Character to count
char *str;
int i = 0;
int count = 0; // Counter
//clrscr();
while(s[i] != '\0') {
if(s[i] == ch)
count++;
i++;
}
printf("\n occurence of substring:");
str=strstr(s,"Chiplun");
printf(" %s ",str);
if(count > 0)
printf("\n %c appears %d time in %s ", ch, count, s);
else
printf("\n %c did not appear in %s ", ch, s);
// getch();
return 0;
}
output:
0 Comments
Post a Comment