answersLogoWhite

0


Best Answer

#include<iostream>

#include<string>

size_t count_spaces(std::string& str)

{

size_t spaces=0;

for(size_t i=0; i<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<<"The string ""<<str.c_str()<<"" has "<<spaces<<" spaces.\n"<<std::endl;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to count number of spaces in a string using array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


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.


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


How do you write a program using function that counts the number of vowels in a string in java language?

Use text-editor notepad++


Write a program in java to display a string message using Servlets?

i dont no string for servlate


Write a program to find the length of a string in shellscript?

This is an assignment not a question.


Write the program in Linux to find the reverse of any string?

i am sam


How do you write a c program to find a word in a string?

what is if(!(str[i]==32))


How do you write a c program to find the smallest word in a string?

what is if(!(str[i]==32))


Write a program convert a string from uppercase to lower case and vice-versa without using string tolower and string toupper commands in TCL?

[ string toupper $str ] or [ string tolower $str ]