may that was a little confusing, heres a newer version with comments except i moved my while loop into a function. so here it is
Code:
#include <stdio.h>
#include <string.h>
int main (void)
{
/*Declare and intialize variables*/
int k = 0;
int sum;
int n;
char *pt;
char *pt2;
char str[256];
char str2[1];
int occurance(char *pt, char *pt2);
/*Get user input and assign to first string*/
printf("Enter sentence. \n");
while((str[k]=getchar()) != '\n')
k++;
str[k]='\0';
n= k + 1;
pt=str;
/*Get from user the character to be counted
and put in 2nd array*/
printf("Enter character to find in sentence. \n");
scanf("%s", str2);
pt2=str2;
sum = occurance(pt, pt2);
printf("Number of times %s occurs: %d \n",str2, sum);
return 0;
}
/*----------------------------------
This function counts the number of times
a specific character occurs */
int occurance(char *pt, char *pt2)
{
/*Initialize variables*/
int count = 0;
/*Count number of times character occurs*/
while((pt=strstr(pt, pt2)) != NULL)
{
count++;
pt++;
}
return (count);
}