I tried the below program .
INPUT-: i want help
Desired OUTPUT-:Words=3 Characters=9
But the actual output deviates from the desired.
Can someone tell what is my mistake .
include
void main()
{
int countch=0;
int countwd=1;
printf("Enter your sentence in lowercase: ");
char ch='a';
while(ch!='\r')
{
ch=getche();
if(ch==' ')
countwd++;
else
countch++;
}
printf("\n Words = ",countwd);
printf("Characters = ",countch-1);
getch();
}
There are few observations which you might find of some use:
1. You are using
getch&getchewhich are both non-standard functions. Make use ofgetcharinstead. In this case as already pointed in unwind’s response you need to useintfor the return type.2. Please change the return type of
mainfromvoidtoint.3. You are not specifying the formats in
printf. Please add%dspecifier to print integers.I have not used codepad but ideone allows you to add inputs to your programs. Here is a reference based on your sample on ideone.
Hope this helps!