answersLogoWhite

0


Best Answer

#include

#include

void main()

{ char str[50];

int i,count,countc;

printf("Enter a string : ");

gets(str);

count=0;

i=0;

while(str[i]!='\0′)

{ if(str[i]==' ')

count++;

i++;

}

printf("The total number of words are %d ",count+1);

}

Read more: http://programmingkid.com/count-number-of-words-in-a-string/#ixzz1aGIR1odb

User Avatar

Wiki User

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

Wiki User

6y ago

#include<stdio.h>

char to_lower (char c)

{

return (c>='A' && c<='Z')?c+32:c;

}

bool is_letter (char c)

{ return (c>='A' && c<='Z') (c>='a' && c<='z');

}

bool is_vowel (char c)

{

if (is_letter(c))

{

switch (to_lower (c))

{

case 'a':

case 'e':

case 'i':

case 'o':

case 'u': return true;

}

return false;

}

bool is_consonant (char c)

{

return is_letter (c) && !is_vowel (c);

}

unsigned count_consonants (char* pstr)

{

char c;

unsigned count=0;

if (pstr) // sanity check!

{

while (c=*pstr++)

if (is_consonant (c))

count++;

}

return count;

}

int main (void)

{

char str[]="The quick brown fox jumps over the lazy dog.";

printf ("%s\n", str);

printf ("There are %d consonants in the string:\n", count_consonants (str));

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include"stdio.h"

int main()

{

char text[50];

int i,v,c;

v=c=0;

puts("Enter a sentence");

gets(text);

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

{

if(text[i]=='a'text[i]=='e'text[i]=='i'text[i]=='o'text[i]=='u')

v++;

else if(text[i]>='a'&&text[i]<='z'text[i]>='A'&&text[i]<='Z')

c++;

}

printf("Vowel=%d\nConsonant=%d\n",v,c);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

size_t count_vowels (char* str)

{

// Note: str MUST be null-terminated!

size_t count = 0;

if (str)

{

for (; *str; ++str)

{

switch (*str)

{

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

++count;

}

}

}

return count;

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

#include<io.sys>

int vowel_count (char* str) {

char c;

int cnt = 0;

if (!str) return cnt;

while (c = *str++) switch (c) {

case 'a': case 'e': case 'i': case 'o': case 'u':

case 'A': case 'E': case 'I': case 'O': case 'U':

++cnt;

break;

}

return cnt;

}

void print_vowel_count (char* str) {

if (str) printf ("The number of vowels in the word '%s' is %d\n", str, vowel_count (str));

}

int main (void) {

print_vowel_count ("geography"); // e.g., 3

print_vowel_count ("history"); // e.g., 2

print_vowel_count ("mathematics"); // e.g., 4

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program to accept a string from user and count number of vowels in the string using pointer to string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


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()


How do you declare a pointer to a character string in c?

char *ptr;


Write a C program to accept a string from user and display its ascii value and then display sum of all ascii value of strings?

//C program to accept a string from user and //display its ascii value and //then display sum of all ascii value of strings #include&lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char String[100]; int Sum,Index; Sum=0; //Sum is initially zero printf("Enter the string:\n"); gets(String); //Accept String from User for(Index=0;Index&lt;strlen(String);Index++) { Sum+=(String[Index]); //Adds (the ASCII values of) the String characters. } printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value. return 0; }


Where string is stored on Heap or Stack in java?

A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.

Related questions

What is inventory pointer c?

// Inventory Pointer // Demonstrates returning a pointer #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; //returns a pointer to a string element string * ptrToElement(vector&lt;string&gt;* const pVec, int i); int main() { vector&lt;string&gt; inventory; inventory.push_back( "sword"); inventory.push_back( "armor"); inventory.push_back( "shield"); //displays string object that the returned pointer points to cout &lt;&lt; "Sending the objected pointed to by returned pointer:\n"; cout &lt;&lt; *(ptrToElement(&amp;inventory, 0)) &lt;&lt; "\n\n"; //assigns one pointer to another - inexpensive assignment cout &lt;&lt; "Assigning the returned pointer to another pointer.\n"; string* pStr = ptrToElement(&amp;inventory, 1); cout &lt;&lt; "Sending the object pointed to by new pointer to cout:\n"; cout &lt;&lt; *pStr &lt;&lt; "\n\n"; //copies a string object - expensive assignment cout &lt;&lt; "Assigning object pointed by pointer to a string object.\n"; string str = *(ptrToElement(&amp;inventory, 2)); cout &lt;&lt; "Sending the new string object to cout:\n"; cout &lt;&lt; str &lt;&lt; "\n\n"; //altering the string object through a returned pointer cout &lt;&lt; "Altering an object through a returned pointer.\n"; *pStr = "Healing Potion"; cout &lt;&lt; "Sending the altered object to cout:\n"; cout &lt;&lt; inventory[1] &lt;&lt; endl; return 0; } string * ptrToElement(vector&lt;string&gt;* const pVec, int i) { //returns address of the string in position i of vector that pVec points to return &amp;((*pVec)[i]); }


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


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.


Why pointer is used to reference a string of characters write C plus plus statements that display the text hello using pointer to a string notation?

We use a pointer to reference a string because a string is an array of characters where every element is a char (or a wchar_t if using UNICODE strings). Passing arrays by value would require the entire array to be copied, but passing a pointer variable to an array only copies the pointer, which is effectively the same as passing the array by reference. #include &lt;iostream&gt; int main() { char * psz = "hello"; // pointer to a null-terminated string. std::cout &lt;&lt; psz; // pass the pointer (by value) to the insertion operator. return( 0 ); }


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()


How do you declare a pointer to a character string in c?

char *ptr;


Write a C program to accept a string from user and display its ascii value and then display sum of all ascii value of strings?

//C program to accept a string from user and //display its ascii value and //then display sum of all ascii value of strings #include&lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char String[100]; int Sum,Index; Sum=0; //Sum is initially zero printf("Enter the string:\n"); gets(String); //Accept String from User for(Index=0;Index&lt;strlen(String);Index++) { Sum+=(String[Index]); //Adds (the ASCII values of) the String characters. } printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value. return 0; }


What is pointer to pointer with examle?

int main (int argc, char **argv):Hereargv is a pointer to a pointer (points to the first element of a pointer-array)argv[0] is a pointer (points to the first character of a string)argv[0][0] is a character


Where string is stored on Heap or Stack in java?

A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.


What is cputs function in computer c plus plus?

Nothing.The C language only recognizes a few keywords, like "for" and "if". Most of what's in a C program ... that doesn't reference routines in the C program itself ... are library calls, and cputs() is one of those. What it does is write its argument (which should be a pointer to a character string) to the console... console put string.


In C programming what is pointer?

Simply put, a pointer is a memory address. It "points" to data at that address. The Data Type of the pointer tells the program how to interpret the data at the address. For example, a character pointer will read the data at the address as a byte and interpret it as a character (like an ASCII character, for instance). A string pointer is just like a character pointer, except that it starts reading characters at the address and keeps going until it encounters a null (a data value of zero). The "string" of characters is interpreted as text. The available data types include just about anything you can imagine. Intrinsic types, structures, functions... in later versions of C, pointers can point to objects.


How to write a program in python to print the reverse of a number?

In python, type the following into a document. NOTE: Sentences following a # symbol are comments, and are not necessary for the program to run. #!/usr/bin/python #This program takes a input from a user and reverses it. number = input("Type a number: ") #This takes input from a user. a = len(number) #This finds the length of the number reverse = "" #This sets the variable reverse to an empty string. for i in number: a = a-1 #The places in a string start from 0. The last value will be the length minus 1.reverse = reverse + number[a] #Makes the number's last digit the new string's first. print("The reverse of", number, "is", reverse + ".") #prints the resulting string. This program will take any sequence of characters and reverse them. The final value is a string, but if an integer is needed, one needs only to add the line reverse = int(reverse) above the print statement. However, this will stop the program from being able to reverse strings, as it is not possible to convert a string to an integer if it is not a number.