Skip to main content
Fixed edit clash
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Counting the number of words in a string

I have written this program to count the number of words in a string. I have checked my program for the worst case-case scenario. If any of you, can find any casecases for which this program doesn't work, please let me know. So so that iI 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 iI have missed something.

Is this a good solution?

How 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;
}

Counting number of words in a string

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.

Is this a good solution?

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;
}

Counting the number of words in a string

I have written this program to count the number of words in a string. I have checked my program for the worst-case scenario. If any of you can find any cases for which 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.

Is this a good solution? 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;
}
deleted 115 characters in body
Source Link

I have written this program to count the number of words in a string. I have checked my program for the worst-case case scenario. If any of you, can find any case for this program doesn't work, please let me know, so. So that Ii 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 Ii have missed something.

I amIs this a beginner, sogood solution?

How can I wanted to ask:improve the code?

  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;
}

I have written this program to count the number of words in a string. I have checked my program for the 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:

  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;
}

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.

Is this a good solution?

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;
}
added 10 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have written this program to count the number of words in a string. I have checked my program for the worst case-case scenario. If any of you, can find any case for this program doesn't work, please let me know. So, so that iI 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 iI have missed something.

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

  1. Is this a good solution?

    Is this a good solution?
  2. Is this a complete solution? Would it work for any case?

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

    #include #include

    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; }

    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;
}

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 #include

    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; }

I have written this program to count the number of words in a string. I have checked my program for the 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:

  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;
}
Source Link
Loading