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

int main(void)
{
    char str1[1000];
    int i, letter, space = 0;
    char ch = str1[i];

    printf("Enter a sentence: ");
    scanf("%[^\n]s", str1);
    printf("you enter %s\n", str1);

    while (i != strlen(str1)) {
        if (ch != ' ') {
            letter++;
        } else if (ch = ' ') {
            space++;
        }
        i++;
    }
    printf("%d %d", letter, space);
}

My while loop isn't working and I can't seem to locate the problem. I am using the terminal in ubuntu and after printing the user string, I get a blank line. I have to use Ctrl-Z to stop the script.

12
  • 4
    i isn't initialized Commented Oct 27, 2016 at 18:04
  • 3
    char ch = str1[i]; this needs to be inside the loop. As now, i and str are uninitilaised. Commented Oct 27, 2016 at 18:05
  • 1
    Try using a debugger... Also turn on compiler warnings to detect many uses of uninitialized variables. Commented Oct 27, 2016 at 18:13
  • 1
    }else if (ch=' '){ is unnecessary, but it's also incorrect. You should be using == instead of = here. Commented Oct 27, 2016 at 18:17
  • 1
    What is this line char ch = str1[i]; supposed to do? Commented Oct 27, 2016 at 18:21

4 Answers 4

7

Mistakes I see: using uninitialised variables - local variables do not get initialised automatically.

Another is you do not read a character from the string within the loop.

The third is the unecessary and syntactically incorrect if (ch=' ') which should have been if (ch==' ')

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

int main(void){
    char str1[1000];
    int i = 0, letter = 0, space = 0;       // initialise all to 0;

    printf("Enter a sentence: ");
    scanf("%[^\n]s",str1);
    printf("you enter %s\n",str1);

    while (i!=strlen(str1)){
        char ch = str1[i];                  // move this inside the loop
        if (ch!= ' '){
            letter++;
        }else {                  // unnecessary - you already checked space
            space++;
        }
        i++;
    }
    printf("%d %d\n", letter, space);
}

Program session:

Enter a sentence: hallo my friend
you enter hallo my friend
13 2
Sign up to request clarification or add additional context in comments.

1 Comment

just realized that was not my updated code. My mistake was thinking i could do int i,letter,space=0; thank you all
2

You need to initialize i to 0 at the beginning of the program.

int i,letter,space = 0;

the above line will only set space to 0 and not i.

1 Comment

str1 needs be initialised as well.
0

This shall work :

int i = 0;
int letter = 0;
int space = 0;
char ch = ' ';

printf("Enter a sentence: ");
scanf("%[^\n]s",str1);
printf("you enter %s\n",str1);

while (i!=strlen(str1)){
   ch = str1[i];
   if (ch!= ' '){
          letter++;
   } else {
         space++;
   }
   i++;
 }

Comments

0

You need to initialise i to 0 before entering the loop. Also if (ch=' ') should be if (ch==' '). Next time, try to compile with warnings enabled (-Wall -Wextra).

Also, while probably not related to the issue here, using strlen() in a condition of a while statement can be a performance disaster if the string is too long. Furthermore, do note that the scanf() function may write beyond the end of the buffer if the input string is too long. For this specific case, I recommend getting the string using getline() (which dynamically allocates the necessary buffer).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.