I need to make a c program that will find The Positions and Frequency of character from a string.
I found the solution...
Code:
#include <stdio.h>
#include <stdlib.h>
int findChar (const char str[], char c)
{
int i, j;
for(i = 0; str[i] != '\0'; i++){
if(str[i] == c){
return i;
}
}
return -1;
}
int findCharFreq(const char str[], int c)
{
int i, j=0;
for(i = 0; str[i] != '\0'; i++){
if(str[i] == c){
j++;
}
}
return j;
}
int main(void)
{
int sCount;
int pos, freq;
char ch, str[80];
printf("Please input some text below: \n");
for(sCount=0; ((str[sCount] = getchar()) != '\n'); ++sCount);
str[sCount+1] = '\0';
printf("Please input your desired character: \n");
ch = getchar();
pos = findChar(str, ch);
freq = findCharFreq(str, ch);
if(pos == -1){
printf("\nWe have not find your desired character in the string.\n");
}
else{
printf("%c is in the postion of %d in the string.\n", ch, pos+1);
printf("And the frequency of %c is %d.\n", ch, freq);
}
system("PAUSE");
return 0;
}
But i have a problem....
This program will display only one (the first) position of a character in a string....
but if a character appears more than once in a string???
how can i make this program to display all the positions of 1 char that appears more than 1 time in a string???
Can anyone help please i'd really be grateful..