-4

I'm using CodeBlocks and C as a programming language. I need to make a function that counts how many times a character read appears in a string (also read). Line 8 error: subscripted value is neither array nor pointer nor vector

#include<stdio.h>
#include<string.h>
int find(char s, char ch, int l)
{
    int i, j=0;
    for(i=0; i<l; i++)
{
    if(strcmp(s[i],ch)==0)
        j++;
}
return j;
}
int main()
{
    char s[30];
    int i,j,l;
    char ch;

    printf("Enter the string: ");
    gets(s);
    l = strlen(s)+1;
    printf("Enter the character: ");
    scanf("%c",&ch);
    j=find(s, ch, l);
    printf("\n%c occurs %d times",ch,j);
    return 0;
} 
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

2

Your error was that in the function declaration you didn't mention that s was an array. You needed to use s[] and there was no need for strcmp function in the if statement.

#include<stdio.h>
#include<string.h>
int find(char s[], char ch, int l)
{
    int i, j=0;
    for(i=0; i<l; i++)
{
    if(s[i] == ch)
        j++;
}
return j;
}
int main()
{
    char s[30];
    int i,j,l;
    char ch;

    printf("Enter the string: ");
    gets(s);
    l = strlen(s)+1;
    printf("Enter the character: ");
    scanf("%c",&ch);
    j=find(s, ch, l);
    printf("\n%c occurs %d times",ch,j);
    return 0;
}