the output has to shown
enter the whole name: Juan DeLa Cruz
No.of characters shown: 14
No. of strings: 3
something like this....
i have no idea about this....just a little about string,i dont know where should i put those...
looking forward for the reply..
How will you count the number of characters including space in a string or group of words using C language?
Should be somethink like this (written in "project style") :
#include %26lt;stdio.h%26gt;
int main(int argc, char* argv[])
{
const unsigned length= 40;
char name[length]; /* declare the variable used to store the name */
unsigned chars=0, strings=0, blank=1; /* declare the counters */
printf("Enter the string :\n");
fgets(name,length,stdin); /* read the string name from the screen */
/* while loop to count chars and string - strlen would be easier but you probably may not use it as it's a project. Stop when it meets the end of the array */
while(name[chars]!='\n' %26amp;%26amp; name[chars]!='\0')
{
if (name[chars]==' ') blank=1; /* remove all the blanks and count them in chars, remembering that we're in a "blank" phase. in case there are many following like in " Juan de la Cruiz " */
else
{
if (chars==0 || blank==1)
{ /* if the first char isn't a blank or after all the blank trailing, we begin a new string in the sentence */
strings++;
blank= 0; /* we're not in a "blank phase" anymore */
}
}
chars++; /* in any case, we advance in the string name */
}
printf("\nNumber of characters in the string is=%d",chars);
printf("\nNumber of strings is=%d\n\n",strings);
}
Reply:Been awhile since I messed with c or c++ but I would count as 14 characters...
plus a null?
isn't there something that will tell you length of string?
strlen
http://www.opengroup.org/onlinepubs/0000...
Reply:strlen()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment