I have written this program to count number of words in a string. I have checked my program for worst case scenario. If any of you, can find any case for this program doesn't work, please let me know. So that i can work and improve it.

And just to be clear, we don't want symbols like, "," , "!" , "?", "." , "\n" to be counted as words. But obviously, "I" should be counted as word, as we consider it in the language. I have made sure of all this by replacing them with spaces. Let me know if i have missed something.

I am a beginner, so i wanted to ask that:

 1. Is this a good solution?
 2. Is this a complete solution? Would it work for any case?
 3. How can i improve the code?


     #include <iostream>
     #include <string> 

    void replace(std::string& str, char x, char y);
    int countWords(std::string x);

    int main(){
	std::cout<<countWords(" \n \t Hello, world ! ");
    }

    void replace(std::string& const str, char x, char y){
	for(unsigned int i=0;i<str.size();i++){
		if(str[i]==x) str[i]=y;
	}
    }

    int countWords(std::string x){

	replace(x,',',' ');
	replace(x,'.',' ');
	replace(x,'!',' ');
	replace(x,'?',' ');
	replace(x,'(',' ');
	replace(x,')',' ');
	replace(x,'\n',' ');
	replace(x,'\t',' ');
	replace(x,'"',' ');

	if(x.empty()) return 0;
	int Num=1;

	for(unsigned int i=1;i<x.size();i++){
		if(x[i]==' ' && x[i-1]!=' ') Num++;
	}
	if(x.back() == ' ') Num--;
	return Num;
    }