answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<string.h>

void main()

{

int count,i,len;

char str[100];

printf("enter the sentence");

gets(str);

len=strlen(str);

for(i=0;i<=len;i++)

{

while(str)

if(str[i]==' ') count++;

}

printf("the number of words are :\t%d",count+1);

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

const char* text; // assuming text is pointed at what you want to search through

const int text_length = strlen(text);

int num_lines = 0; // number of line we've found

// iterate through text characters

int i;

for(i = 0; i < text_length; ++i) {

// increment counter if we found a newline character

// (this is assuming you are using \n to separate your lines...

// if not, change the character to whatever you happen to be using)

if( text[i] == '\n' ) {

++num_lines;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<string.h>

void main()

{

int c=0,i,j,f;

char str1[50];

char str2[10];

printf("enter the string :-\n");

gets(str1);

printf("enter the word you want to count of maximum 10 characters........\n");

gets(str2);

for(i=0;i<strlen(str1);)

{

j=0,f=0;

while(j<strlen(str2))

{

if(str1[i++]==str2[j++])

{

f=1 ;

}

else

{

f=0;

i--;

break;

}

}

if((f==1)&&(i==strlen(str1)str1[i]==' '))

c++;

i++;

}

printf("the total occurrences are:=%d",c);

}

~

BY : rohit verma

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

*program to count number of words, lines and characters in given text*/

#include<stdio.h>

#include<conio.h>

void main()

{

char text[200];

int i,c=0,w=0,l=0;

clrscr();

printf("\nEnter text :(To terminate text press 'ENTER key' then 'cntl+z') :\n\n");

scanf("%[^z]",text);

for(i=0;text[i]!='\0';i++)

{

if(text[i]==' 'text[i]=='.')

{

c++;

w++;

}

else if(text[i]=='\n')

{

w++;

l++;

}

else

{

c++;

}

}

printf("\nNo. of characters=%d\n",c);

printf("\nNo. of words=%d\n",w);

printf("\nNo.of lines=%d\n",l);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

You don't actually need an array to count words in a sentence. Simply traverse the sentence (string) one character at a time until you encounter a non-whitespace character. Increment the count. Now traverse to the next whitespace. Repeat until you reach the end of the string. Return the count.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Write a C program to count the number of characters in a given string with and without using srtlen() function

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Write a java program using IO String to count the number of words in a file.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program To count number of words in a given string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......


What program can be written to count the number of alphanumerics in a string?

public int getStringLength(String val) { return val.length(); } There is an inbuilt functionality in strings that counts the number of alphabets in a string called length()


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }


How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


Write a program to count number of spaces in a string using array?

#include&lt;iostream&gt; #include&lt;string&gt; size_t count_spaces(std::string&amp; str) { size_t spaces=0; for(size_t i=0; i&lt;str.size(); ++i) if( str[i]==32 ) ++spaces; return( spaces ); } int main() { std::string str("This is a string with some spaces."); size_t spaces = count_spaces(str); std::cout&lt;&lt;"The string ""&lt;&lt;str.c_str()&lt;&lt;"" has "&lt;&lt;spaces&lt;&lt;" spaces.\n"&lt;&lt;std::endl; }

Related questions

Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......


How do you write c program to accept a string from the console and count number of vowels constants digits tabs and blank spaces in a string?

Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.


What program can be written to count the number of alphanumerics in a string?

public int getStringLength(String val) { return val.length(); } There is an inbuilt functionality in strings that counts the number of alphabets in a string called length()


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }


How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


Write a c program to count the number of word in a string?

#include&lt;stdio.h&gt; void main() { int cnt=0,i; char str[100]; printf("Enter the string "); scanf("%s",str); for(i=0;i&lt;strlen(str)-1;i++) { if(str[i]==' ') cnt++; } printf("\nTotal no. of word in string = %d",cnt); }


It 4-1 solved question papers November -2008?

Write a program to count the number of IS in any number in register B and put the count in R5.


Write a program to count number of spaces in a string using array?

#include&lt;iostream&gt; #include&lt;string&gt; size_t count_spaces(std::string&amp; str) { size_t spaces=0; for(size_t i=0; i&lt;str.size(); ++i) if( str[i]==32 ) ++spaces; return( spaces ); } int main() { std::string str("This is a string with some spaces."); size_t spaces = count_spaces(str); std::cout&lt;&lt;"The string ""&lt;&lt;str.c_str()&lt;&lt;"" has "&lt;&lt;spaces&lt;&lt;" spaces.\n"&lt;&lt;std::endl; }


What is the program to find the number of each word in the string array?

#include&lt;iostream&gt; #include&lt;string&gt; using namespace std; int main() { int count=0; string b; string a[6]={"technical","school","technical","hawler","school","technical"}; for(int i=0;i&lt;6;i++) { b=a[i]; for(int j=i;j&lt;6;j++) { if(a[j]==b) count++; } cout&lt;&lt;a[i]&lt;&lt;" "&lt;&lt;count&lt;&lt;endl; count=0; } return 0; }


How to write a C Program that will count the number of times 1 occur?

There are some ways to do it. Here I give you an example. You can do it if you take the input as string. #include &lt;stdio.h&gt; #include &lt;string.h&gt; main(void) { int i, count = 0; char ch[10000]; gets(ch); int len = strlen(ch); for(i = 0; i &lt; len; i++) { if(ch[i] == '1') { count++; } } printf("Number of times one occurs: %d\n", count); }


Write a program to accept a string from keyboard and count and display the numbers of vowels present in the string by using function?

You need to scan through the string and keep track of the vowelsoccurring. Here is a sample program:#include#includeint countVowels(char[] s){int count = 0, i;for( i=0; char[i] != '\0'; i++){switch(char[i]){case 'a':case 'e':case 'i':case 'u':case 'o': count++;break;}}return count;}int main(){char str[256];printf("Enter the string:\t");scanf("%s", str);printf("The number of vowels in the string are :%d\n", countVowels(str));return 0;}


Write a java program to input a string and find the total number of uppercase letters in the string?

The code is given at the bottom. This code requires JAVA 1.5+ version to be compiled.This is very simple program. We ask user to input string and later we just iterate through all the character in the string. We use Character.isUpperCase() static method to check is the character we are checking is upper case if so we increase count variable by one. After iteration is done we print count variable and application quits.Note: for statement was introduced in 1.5 JAVA version.Example of usage:david-mac:~ david$ java TestEnter string: This is A Test String.Your string has 4 upper case letters.-------------------------import java.io.*;import java.lang.*;public class Test{public static void main(String[] args)throws IOException{BufferedReader in = new BufferedReader(new InputStreamReader(System.in));System.out.print("Enter string: ");String userString = in.readLine();int count = 0;for (char ch : userString.toCharArray()){if (Character.isUpperCase(ch)){count++;}}System.out.println("Your string has " + count + " upper case letters.");}}