answersLogoWhite

0

#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

Still curious? Ask our experts.

Chat with our AI personalities

JudyJudy
Simplicity is my specialty.
Chat with Judy
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran

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