Click here to Skip to main content
15,032,256 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include<stdio.h>
void main()
    {
    char name[40];
    printf("Enter the string:");
    gets(name);
    char ch = ' '; 
    int i,count=0;
    for(i=0; name[i]; i++)
       {
       if(name[i]==' ')
          count++;	
       }
    printf("Number of words is %d",(count)+1);
}


What I have tried:

tried understanding several times but could not figure it out
Posted
Updated 31-May-20 9:19am
v2
Comments
Patrice T 31-May-20 14:57pm
   
Give details, which part of loop you don't understand ?
Member 14849246 31-May-20 15:04pm
   
test condition what it is trying to convey i mean something compare with i< like that but its just char
Richard MacCutchan 1-Jun-20 4:24am
   
This is the same question you posted in the C/C++ forum. Please do not cross post.
KarstenK 1-Jun-20 4:34am
   
Your counter will miss the last word, when no space is the last character. And when the user gives more than 40 characters it will crash.

First off, indent and space your code - it makes it a lot more readable. (I've done that for your question - see how much easier it is to see what is going on?)

The for loop is simple:
C++
for (a; b; c)
   d
1) Before the loop starts, the statement a is executed to initialise the loop.
2) Then the statement b is ecxcuted - if it returns a non-zero value the loop body d is executed, if not the loop exits.
3) After each time d has been executed, c is executed to advance the loop to the next iteration, and the process continues from (2) above.

Nice and simple, just like your code...
If you still don't understand your loop, then use the debugger to step through the loop while it is running and watch exactly what happens - that should help you to work it out in a few seconds.
   
Quote:
test condition what it is trying to convey i mean something compare with i< like that but its just char

Ah! That's pretty simple: C is a very old language, and it has no concept of boolean values. Instead, it works on zero / nonzero: any nonzero value is "true" and zero alone is "false".

So your loop condition is checking the character for "zeroness" - and because C is an old language it doesn't have and concept of strings either: instead an array of characters needs a "null character" '\0' at the end of the data to tell it when the string ends. So "Mike" is a five character array:
C++
arr[0] = 'M'
arr[1] = 'i'
arr[2] = 'k'
arr[3] = 'e'
arr[4] = '\0'
Without the null terminator, the characters would just continue to be looked at until the program crashed or found a random null in memory somewhere.
   
Comments
Member 14849246 31-May-20 15:18pm
   
if we give input perm0n0nt then program should give count 2 , which is wrong
Quote:
I dont understand the loop logic update condition

The loop
C++
for(i=0; name[i]; i++)

is the same as
C++
for(i=0; name[i]!=0; i++)

In C, a string as a char array is 0 terminated.
This
C++
char name[40];

means that name can be 39 useful chars + 1 char of value 0 which tells that the string is terminated.

This is combined with the fact that Boolean type does not exist in C, but is simulated with value non 0 is meant true, and value 0 is meant false.
From C point of view, a char is an 8 bits integer.
Quote:
when we press enter is it symbol of'\0' which is zero to c compiler?

It is not related to Enter.
Have a look at escaped chars in C.
Escape sequences in C - Wikipedia[^]
Escape Sequences in C - GeeksforGeeks[^]
   
v3
Comments
Member 14849246 31-May-20 15:29pm
   
when we press enter is it symobol of'\0' which is zero to c compiler?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900