Looking for a better word replacement program in cpp ,than mine

Aug 31, 2012 at 10:15am


Hi all, I have written a word replacement program in c++. Can anyone write a better solution than this? Because this uses character arrays .

#include <stdio.h>
#include <iostream>
#include <string>

#include <vector>
using namespace std;

int main()
{

string str;

vector<string> a;
vector<string>::iterator it;

char word[40];

cout<<"Please enter something :"<<endl;

gets(word);

int pos;

int length = strlen(word);


// copy the sentence into a vector
for(int i=0;i<length;i++)
{
str.push_back(word[i]);

if(word[i] == ' ' || i == length - 1 )
{
a.push_back(str);
str.clear();
continue;
}

}


cout<<"Please enter another string: "<<endl;

cin>>str;


cout<<"Enter the position to replace the word:"<<endl;
cin>>pos;


it = a.begin();

it = a.erase(it+pos);

it = a.begin();

a.insert(it+pos,str);


for(it = a.begin();it <a.end();it++)
{
cout<<*it<<" ";
}

cout<<endl;

system("pause");

}

Aug 31, 2012 at 10:34am
I'm not sure what that's meant to be doing, but it doesn't feel quite right.

1. Why are you using a char[40] for a string when you could use std::string? You use string elsewhere, so why not there?

2. Why are you mixing gets and std::cout? Do you realise that std::cin and std::cout are tied?

3. Please the code format tags to format your code.

4. Please describe what the program is supposed to be doing.
Aug 31, 2012 at 11:03am

Output of the program
enter the word : I am a girl
Enter another string : boy
Enter the position to replace the word : 3

I am a boy .

This is the output .

Brief explanation :

I have initially taken the word into a character array and have copied it to a vector<string> , Then I have taken another string.
I have finally taken a position value from the user and have replaced the second word with position value .
Last edited on Aug 31, 2012 at 11:03am
Aug 31, 2012 at 2:37pm
1. Read the strings into an ordered collection (like a vector).
2. Use the index to identify the element of the collection to be replaced.
3. Overwrite the indexed element with the new word.
4. Write out the sentence from the collection.
Aug 31, 2012 at 2:46pm
I already said you in your previous theme how your task can be done simply by using std::istringstream.

s.replace( s.find( source ), source.size(), target );

All what you need is to extract the source word from istringstream.
Aug 31, 2012 at 5:09pm
I suppose one shouldn't be just destroying white spaces in the original string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <algorithm>
#include <iostream>

std::string replace( std::string str, std::string::size_type pos,
                     const std::string& replacement )
{
    auto start = str.begin() ;
    while( pos > 0 )
    {
        static const auto isspace = []( char c ) { return std::isspace(c) ; } ;
        static const auto isnotspace = []( char c ) { return !std::isspace(c) ; } ;

        auto f = std::find_if( start, str.end(), isspace ) ;
        start = std::find_if( f, str.end(), isnotspace ) ;
        if( start != str.end() ) --pos ;
        else break ;
    }

    if( start != str.end() )
        str.replace( start, std::find_if( start, str.end(), isspace ), replacement ) ;
    return str ;
}

int main()
{
    std::cout << replace( "I    am \t\t a    girl", 3, "boy" ) << '\n' ;
}



TO DO: Handle punctuation correctly.
"I am - repeat am - a girl."
Topic archived. No new replies allowed.