Home > Net >  Segmentation fault in alphabet counter function
Segmentation fault in alphabet counter function

Time:01-17

I am writing a C program that's supposed to take a char array and then count all the lowercase letters in it, all the uppercase letters in it as well as all the vowels. For some reason though, it's not running because the compiler returns a segmentation fault. I don't know what it is and I don't know where the problem is


#include <stdio.h>
int lccount(char x[10]){
    int count=0,i,j;
    char lowalphabet[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    for(i=0;i<=10;i  ){
        for(j=0;j<=26;j  ){
            if(x[i]==lowalphabet[j]){
                count  ;
            }
        }
    }
return count;}
int uccount(char x[10]){
    int count=0,i,j;
    char upalphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    for(i=0;i<=10;i  ){
        for(j=0;j<=26;j  ){
            if(x[i]==upalphabet[j]){
                count  ;
            }
        }
    }
return count;}
int vcount(char x[10]){
    int count=0,i,j;
    char vowels[]={'a','e','i','o','u','A','E','I','O','U'};
    for(i=0;i<=20;i  ){
        for(j=0;j<=10;j  ){
            if(x[i]==vowels[j]){
                count  ;
            }
        }
    }
return count;
}
void main(){
    char x[10]={'a','W','E','R','s','d','a','e','i','A'};
    int v,uc,lc;
    v=vcount(x[]);
    uc=uccount(x[]);
    lc=lccount(x[]);
    printf("%d vowels\n%d uppercase\n%dlowercase",v,uc,lc);
}

Result => segmentation fault

CodePudding user response:

First of all, your main() signature is non-standard. Secondly, you pass arrays as params only with their identifiers.

int main(int argc, char *argv[])
{
    char x[10]={'a','W','E','R','s','d','a','e','i','A'};
    int v,uc,lc;
    v=vcount(x);
    uc=uccount(x);
    lc=lccount(x);
    printf("%d vowels\n%d uppercase\n%dlowercase",v,uc,lc);
    return 0;
}

Keep in mind there are a lot of other problems in your code. For example the fact it shows 8 lowercase when in fact there are 6. Check the comments the others posted in your question regarding loops as a hint to fix it.

CodePudding user response:

As others pointed out in the comments, your code has a buffer overflow. Arrays in C are indexed starting from 0, which means that, if you have an array of (say) 26 characters, the first character is at position 0, the second at position 1, ..., the ith at position i-1, the 26th at position 25:

for (int i = 0; i < 25;   i)
    ...

And you don't pass a string to a function with [].

Also, your main() declaration is not correct. It should be either one of these:

  1. int main(int argc, const char *argv[]) if you use arguments from the command line, or
  2. int main(void) otherwise.

There are a couple of things you can do to improve your code:

  • You can avoid allocating an array of chars and the nested for loop if you compare the ith character with its ASCII code.
  • You can pass the size of the array/string you want to process as a function argument.
  • Always use const when you don't modify your data.
int lccount(const size_t size, const char x[size])
{
    int count = 0;

    for (size_t i = 0; i < size;   i)
        if (x[i] >= 'a' && x[i] <= 'z')
              count;
    
    return count;
}

int uccount(const size_t size, const char x[size])
{
    int count = 0;

    for (size_t i = 0; i < size;   i)
        if (x[i] >= 'A' && x[i] <= 'Z')
              count;
    
    return count;
}

int vcount(const size_t size, const char x[size])
{
    int count = 0;
    for (size_t i = 0; i < size;   i)
        switch (x[i]) {
        case 'a': case 'A':
        case 'e': case 'E':
        case 'i': case 'I':
        case 'o': case 'O':
        case 'u': case 'U':
              count;
            break;
        default: continue;
        }
    
    return count;
}

int main(void) // Since you don't use argc and argv
{
    char x[10] = {'a', 'W', 'E', 'R', 's', 'd', 'a', 'e', 'i', 'A'};
    const size_t size = sizeof(x) / sizeof(x[0]);

    int v = vcount(size, x);
    int uc = uccount(size, x);
    int lc = lccount(size, x);

    printf("%d vowels\n%d uppercase\n%d lowercase",v, uc, lc);
}

Result:

6 vowels
4 uppercase
6 lowercase
  •  Tags:  
  • c
  • Related