-2

I want to make this program check if ch is all capital and got no space but I have a problem in ver if i type all capital it starts to add multiple numbers or idk

#include<stdio.h>
#include<string.h>
void main(){
    char ch[200];
    int i=0,ver=0;
    do {
        printf("Donner un chaine : ");  
        gets(ch);
        int x=strlen(ch)-1;
        do{
            printf("ch[x]=%c\t",ch[x]);
            printf("x=%d\t",x);
            printf("ver=%d\n",ver);         
            if (ch[x]==' '){
                x-=1;
                ver+=1;
            }
            else if('a'<=ch[x]<='z'){
                x-=1;
                ver+=1;
            }
            else{
                ver+=0;
            }
        }while(x>-1);   
    }while(ver>0);
}
Jino Michel Aque
  • 513
  • 1
  • 4
  • 16
  • 1
    `if('a' <= ch[x] <= 'z')` is wrong. Mathematics expressions do not work like that in C. Apart from that I suggest you take a look at [`islower()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/islower-iswlower-islower-l-iswlower-l?view=msvc-170) and [`isupper()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/isupper-isupper-l-iswupper-iswupper-l?view=msvc-170). – Weather Vane Nov 05 '22 at 23:14
  • 1
    Also, function `gets()` is obsolete and is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Nov 05 '22 at 23:16

1 Answers1

1
  1. Use functions
  2. Do not use gets only fgets
  3. Use standard functions checking if letter is uppercase or lowercase
int isUpperCase(const char *str)
{
    while(*str) if(islower((unsigned char)*str++)) return 0;
    return 1;
}

int containsSpace(const char *str)
{
    while(*str) if(*str++ == ' ') return 1;
    return 0;
}

void print(const char *str)
{
    printf("All letters are %suppercase and it does %scontain spaces\n", isUpperCase(str) ? "" : "not ", containsSpace(str) ? "" : "not ");
}

int main(void)
{
    print("ASDFGHJKJG");
    print("ASDFG HJKJG");
    print("ASDF435345%$^%$#^$%#^$%$^$ GHJKJG");
    print("ASDF435s345%$^%$#^$%#^$%$^$ GHJKJG");

}

https://godbolt.org/z/5Kqj4o4oa

0___________
  • 60,014
  • 4
  • 34
  • 74