Hello, I'm simply trying to keep track of how many times a space is encounteed in a string and then print the number of times to the console. Can someone please examine and give me a tip on what is wrong? Thank you!

#include <iostream>
using namespace std;


int main()
{
	int count = 0;
// char newphrase[1024];

char phrase[1024];
cout << "enter phrase" << endl;
cin >> phrase;
int b = strlen(phrase);
for(int i = 0; i <= b; i++)
{
	if(phrase[i] == ' ')
	{
		count++;
		
	}
	/*
	{
		strcpy_s(phrase, newphrase);
		
	}
*/
	


}
cout << count << endl;
return count;

}

Recommended Answers

All 4 Replies

Your call to "cin >> phrase" will by default copy the string before the first space character. Just replace it with "cin.getline(phrase,1024,'\n');" and it will work.

Your call to "cin >> phrase" will by default copy the string before the first space character. Just replace it with "cin.getline(phrase,1024,'\n');" and it will work.

Thak you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.