简体   繁体   中英

C++ Replacing a word in an array of characters

I'm working on a problem where I need to have user input a message then replace the work "see" with "c". I wanted to read in the array message[200] and then break it down into individule words. I tried a for loop but when I concatinate it just adds the privous words. I am only to use array of characters, no strings.

const int MAX_SIZE = 200;

int main(){

    char message[MAX_SIZE]; //message array the user will enter
    int length;     // count of message lenght
    int counter, i, j;      //counters for loops
    char updateMessage[MAX_SIZE];   //message after txt update

    //prompt user to
    cout << "Please type a sentence" << endl;
    cin.get(message, MAX_SIZE, '\n');
    cin.ignore(100, '\n');

    length = strlen(message);
    //Lower all characters
    for( i = 0; i < length; ++i)
    {
        message[i] = tolower(message[i]);


    //echo back sentence
    cout << "You typed: " << message << endl;
    cout << "Your message length is " << length << endl;

    for( counter = 0; counter <= length; ++counter)
    {

            updateMessage[counter] = message[counter];

            if(isspace(message[counter]) || message[counter] == '\0')
            {
                    cout << "Space Found" << endl;
                    cout << updateMessage << endl;
                    cout << updateMessage << " ** " << endl;

            }
    }
return 0;
}

After each space is found I would like to output one work each only.

You should really try to learn some modern C++ and standard library features, so you don't end up writing C code in C++. As an example, this is how a C++14 program makes use of standard algorithms from the library to do the job in 10-15 lines of code:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    using namespace std::string_literals;

    std::istringstream input("Hello I see you, now you see me");
    std::string str;

    // get the input from the stream (use std::cin if you read from console)
    std::getline(input, str);

    // tokenize
    std::vector<std::string> words;
    std::istringstream ss(str);
    for(std::string word ; ss >> word; words.push_back(word));

    // replace
    std::replace(words.begin(), words.end(), "see"s, "c"s);

    // flatten back to a string from the tokens
    str.clear();
    for(auto& elem: words)
    {
        str += elem + ' ';
    }

    // display the final string
    std::cout << str;
}

Live on Coliru

This is not the most efficient way of doing it, as you can perform replacement in place, but the code is clear and if you don't need to save every bit of CPU cycles it performs decently.

Below is a solution that avoids the std::vector and performs the replacement in place:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    std::istringstream input("Hello I see you, now you see me");
    std::string str;

    // get the input from the stream (use std::cin if you read from console)
    std::getline(input, str);

    // tokenize and replace in place
    std::istringstream ss(str);
    std::string word;
    str.clear();
    while (ss >> word)
    {
        if (word == "see")
            str += std::string("c") + ' ';
        else
            str += word + ' ';
    }

    // display the final string
    std::cout << str;
}

Live on Coliru

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM