adohertyd
adohertyd

Reputation: 2689

In C, use a while loop to count the number of characters a user inputs

I'm a complete newbie to programming and am having a lot of trouble with something I know is very basic. I create a program where the user inputs a word and I must use a while loop to calculate the number of characters in the word and show the result on screen.

I'm fine with having the user input the word but my problem is with the while loop. I just can't understand how to code it. I really would appreciate some help with this.

Thanks

Edit:

Here's what I've done so far:

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

int main(void)
{
char input[30]; 
int wordlen;

printf("please enter a word: \n");
scanf("%29c", input);

while (input < 30);
{
/*Not sure what to put in here*/
printf("Number of letters in input is %s", /*???*/);
}

return 0;
}

Another edit: This is homework but my lecturer is rubbish and doesn't explain things well. I'm trying to learn and would like to understand how it works I'm not necessarily expecting a direct answer. Even some hints as to how to solve it myself would be great. Thanks


Ok after much trial and error here is what I have come up with. I think it is correct but would like your opinions on it. Please bear in mind I have been doing C for less than 3 weeks so my technique might be poor. Thanks for your input everyone.

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

int main(void)
{
char input[30]; 
int i;
int x;
x=0;

printf("please enter a word: \n");
scanf("%29s", input);
i=strlen(input);

while (x < i)
{
 x++;
}
 printf("Number of letters in input is %d", x);


return 0;
}

Upvotes: 2

Views: 25883

Answers (4)

kbyrd
kbyrd

Reputation: 3351

In the spirit of a homework tagged post, I've deleted my implementation and I'll offer hints instead:

Strings in C are NULL terminated, that means that if you have a string like "qW3rTy" pointed to by a char[30], in memory this actually looks like this:

input[0] = 'q'
input[1] = 'w'
input[2] = '3'
input[3] = 'r'
input[4] = 'T'
input[5] = 'y'
input[6] = '\0'
... // It doesn't matter what's after here, because the NULL above marks the end.

That '\0' is another way of saying that the value at that byte is literally zero, aka NULL. So, to count the number of characters in a string, you loop over the string looking for the first null character, incrementing a counter. Be sure you don't count the NULL.

Upvotes: 4

BLUEPIXY
BLUEPIXY

Reputation: 40145

i = strlen(input);
while (x < i){
    x++;
}

You can count up to a count of x in conjunction with strlen does not make sense. Only just x = i. If the purpose of counting itself is, to count the actual characters without strlen.

ex.)

while (input[x] != '\0'){
    x++;
}

Upvotes: 0

BLUEPIXY
BLUEPIXY

Reputation: 40145

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

int main(void){
    char input[30]; 
    int wordlen;

    printf("please enter a word: \n");
    // %29c is request 29 chars include newline
    // %29s is max 29 chars input exclusive space chars(space, tab, newline..)
    // %*c  is drop out char for newline ('\n' remain buffer)
    // %s is request of C string
    // C string is char sequence , and last char is '\0'
    scanf("%29s%*c", input);
    // strlen is counting C string chars until last '\0' char
    wordlen = strlen(input);

    //while(condition); //this while does not has  block to execute
    //input is constant address , The address comparison(input < 30) is meaningless
    //while break condition as 0==strlen(input) is NG (why reason scanf wait until input)
    //exit loop when input special string "-quit"  
    while (0!=strcmp("-quit", input)){
        printf("Number of letters in input is %d.\n", wordlen);
        printf("please enter a word: \n");
        scanf("%29s%*c", input);
        wordlen = strlen(input);
    }

    return 0;
}

Upvotes: -1

derekhh
derekhh

Reputation: 5552

If you are storing the string in a character array, say s, the easiest solution is to use built in C-function called strlen, which can compute the number of characters in a string

printf("%d\n",strlen(str));

Using a while loop, you need to check from the start of the array until you reach a NULL('\0') character, which goes something like:

len = 0;
while(str[len] != '\0')
  len++;
printf("%d\n",len);

Upvotes: 0

Related Questions