2

Only the 'strlen' function is being executed by the program.

The if statements inside this while loop do not even work...

#include<stdio.h>
#include<string.h>
#include <ctype.h>

main()
{
    char cMessage[100];
    int cLow = 0, cUp = 0, cSpec = 0, cSpace = 0, cNum = 0;
    printf("Enter your message: ");
    scanf("%s", cMessage);
    int x = 0;

    while(cMessage[x] != 0)
    {
        x = strlen(cMessage);
        printf("Total characters: %d", x); 

        if(islower(cMessage[x]))
        {
            printf("\nTotal Lowercase Letters: %d", cLow);
            cLow++;
        }
        else if(isupper(cMessage[x]))
        {
            printf("\nTotal Uppercase Letters: %d", cUp);
            cUp++;
        }
        else if(isalnum(cMessage[x]))
        {
            printf("\nTotal Special Characters: %d", cSpec);
            cSpec++;
        }
        else if(isspace(cMessage[x]))
        {
            printf("\nTotal Lowercase Letters: %d", cSpace);
            cSpace++;
        }
        else if(isdigit(cMessage[x]))
        {
            printf("\nTotal Lowercase Letters: %d", cNum);
            cNum++;
        }
    }
    x++;
}

I cannot figure out the cause of this issue...

What may be the cause of this?

EDIT: So here's the revised code of the program, the only problem that I have now is that the spaces are not being counted. And btw, is there a specific function used to 'count' special characters? I've used 'isalnum' and I realized it was wrong

#include<stdio.h>
#include<string.h>
#include <ctype.h>
#include<conio.h>

main(){
char cMessage[100];
int cLow=0, cUp=0, cSpec=0, cSpace=0, cNum=0;

printf("Enter your message: ");
scanf("%s", cMessage);
int x=0;
while(cMessage[x]){

printf("Total characters: %d", strlen(cMessage)); 
while(cMessage[x]!=0){
if(islower(cMessage[x])){ cLow++;}

if(isupper(cMessage[x])){ cUp++;}

if(isalnum(cMessage[x])){ cSpec++; }

if(isspace(cMessage[x])){ cSpace++; }

if(isdigit(cMessage[x])){ cNum++; }
x++;
}
printf("\nTotal Lowercase Letters: %d", cLow);
printf("\nTotal Uppercase Letters: %d", cUp);
printf("\nTotal Special Characters: %d", cSpec);
printf("\nTotal Spaces: %d", cSpace);
printf("\nTotal Numbers: %d", cNum);
getch();
   }
}
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
Ed Hernandez
  • 31
  • 1
  • 6
  • 1
    Why is this tagged C++? this is pure C – JVApen May 06 '18 at 11:27
  • If you are learning C/C++ programming, you should use a better IDE, it helps out A LOT. I personally recommend Visual Studio Community Edition. It takes some time to learn it, but totally worth it. Debugging with breakpoints is an excellent exercise on how to identify bugs in your source code, like the one in your question. – sɐunıɔןɐqɐp May 06 '18 at 11:29
  • I suppose you are trying to count each type of character in your string. In this case, put the result of strlen() into a separate variable, outside the loop. Then try to use a for(int x=0; x – sɐunıɔןɐqɐp May 06 '18 at 11:36
  • Yes that's what I'm trying to do. Unfortunately can't use any other type of loops since the 'while' loop is the one assigned to me. – Ed Hernandez May 06 '18 at 11:44
  • 2
    What do you expect? You overwrite your counter `x` with `x = strlen(cMessage);` at the beginning of the loop. – Killzone Kid May 06 '18 at 11:44
  • 1
    I'm currently using DevC++ since it's the one being used by the class. I won't probably invest more time into learning C++ since IT isn't my major, but we're required to learn programming so there's that – Ed Hernandez May 06 '18 at 11:46
  • @EdHernandez: you can write any for loop as a while loop and vice-versa. I recommend doing some research about this, preferably in English. – sɐunıɔןɐqɐp May 06 '18 at 11:58
  • @EdHernandez: my answer below tells you how to include spaces and tells you how to count special characters. focus on the last paragraphs. – Shadi May 06 '18 at 14:12

3 Answers3

3

The x = strlen(cMessage); will give you the length of cMessage which is always the index of the last item + 1.

for example if: cMessage = "The" and x = strlen(cMessage) , then:

x = 3
cMessage[0] = 'T'
cMessage[1] = 'h'
cMessage[2] = 'e'
cMessage[3] = NULL terminator // equivalence to 0

Note that there is usually a NULL terminator after the last character.

So as you can see, the while condition is always false after the first pass.

Try to use a separate variable to iterate through cMessage.

Also you need to consider putting variables like cUp++' before the 'printf statements.

A more elegant alternative will be using for statement instead of while.

Also note that isalnum(cMessage[x]) is interfering with if(isdigit(cMessage[x])) so, it is better to use separate if statements and git rid of else if, moreover, if you want to count special characters you have to negates isalnum to be: if(!isalnum(cMessage[x])).

At last your input will not accept sentences (word with spaces between them), so you have to consider replacing:

scanf("%s", cMessage);

with

scanf("%[^\n]s",&cMessage);
Shadi
  • 1,701
  • 2
  • 14
  • 27
2

cMessage[x] after x=strlen(cMessage) is always 0. String contains chars from 0 till x - 1, char at post x is 0. Thus any if-condition is false.

I suppose x=strlen(cMessage); is not needed and must be removed, x++ must be three last operator in the loop body, since you want to count chars of different kinds.

printf("Total characters: %zu", strlen(cMessage));
while(cMessage[x] != 0) {
    if(islower(cMessage[x])) {
    ...
    }
    x++;
}

%d is not proper format for size_t type on 64-bit platform. Read this: How can one print a size_t variable portably using the printf family?

273K
  • 29,503
  • 10
  • 41
  • 64
2

You take x as the lenght of your string. So that's one longer then your Array. After the full array is always a 0 to show the program the array is not longer. This way you can never go in the if's

Stan Fieuws
  • 162
  • 1
  • 2
  • 13