How would I write this program (detailed description in content)?

Lets say my C++ program asks the user for a list of words or a file containing words. The user inputs as many words they want, and we don't know how many letters each one of these words take up. It could be a 5 letter word or even a 20 letter word.

There is one restriction for the user that inserts the inputs however. Every word ends in 3 possible ways. Lets say the "main" part of the word the user inputs are **house car plane** (Any other word can be entered and any amount of words can be entered. This is just an example). These three words are unique, but they can't be entered like this. These three words must be followed up by **red, yellow, or blue**. These three endings to a "main" part of a word are expected.

In short, only 9 words can be entered into the program in this example. These words are **housered houseyellow houseblue carred caryellow carblue planered planeyellow planeblue**. If the user entered shovel instead of house, car or plane, they would enter **shovelred, shovelyellow, or shovelblue** (Any of these three, it doesn't matter). The "main" part of the word will *always* be followed by red, yellow, and blue no matter what the word is and this is not decided by the program but by the person that makes the input.

Now what the program actually does is this.

The "red" part of any variation of these strings will be removed and replaced by one, two, three, four, five , and six as an output.

The "yellow" part of any variation of these strings will be removed and replaced with uno, dos, tres, cuatro, cinco, and seis as an output.

The "blue" part of any variation of these strings will be removed and replaced with eins, zwei, drei, vier, funf, and sechs as an output.

--------------------

**This could be an example**

cout << "Please enter your words: ";

//User enters guitarblue

//The output would look like this

guitareins guitarvier

guitarzwei guitarfunf

guitardrei guitarsechs

//these 3 lines are displayed and program ends

//the user enters bookyellow

bookuno bookquatro

bookdos bookcinco

booktres bookseis

//these 3 lines are displayed and the program ends

-----------


I hope I was able to explain what I'm trying to do. I honestly have no idea how to do this. The most difficult thing that I've literally spent hours trying to figure out is how the program knows what color follows the main part of the word. I have absolutely no idea how to do this. Do I use a string, a cstring, or a char and put it into an array or something? How can the last word (or last 5 characters) of the array be read?

**Also notice how blue, yellow, and red all have different amount of characters. Lets pretend magically that these three words are all 5 characters long so I guess you can count back the same amount of spaces in an array. I only used colors because I thought it would be easier to understand. This is important! The colors just represent three different words that are 5 characters long each!


I will check back in a few hours so please don't get mad at me if I don't reply immediately! I need to rest.
Something like this :
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int i;
int main()
{
    char word[51];
    char mainpart[51];
    char suffix[4];
    cout<<"Enter a word : ";
    cin>>word;                              //input the word
    int wordlen=strlen(word);
    for(i=1;i<4;i++)                        //
    {                                       // Get the last three letters for yellow,
        suffix[3-i]=word[wordlen - i];      // red or blue
    }
    strcpy(mainpart,word);
    if(strcmp(suffix,"low")==0)             //Check the suffix
    {                                       //and output the words respectively
        mainpart[wordlen-6]=0;              //remove the suffix
        strcpy(word,mainpart);
        strcat(word,"uno");
        cout<<word<<endl;
        strcpy(word,mainpart);
        strcat(word,"dos");
        cout<<word<<endl;
        .
        .
        .
    }
    else if(strcmp(suffix,"red")==0)
    {
        mainpart[wordlen-3]=0;              //remove the suffix
        strcpy(word,mainpart);
        strcat(word,"one");
        cout<<word<<endl;
        strcpy(word,mainpart);
        strcat(word,"two");
        cout<<word<<endl;
        .
        .
        .
    }
    else if(strcmp(suffix,"lue")==0)
    {
        mainpart[wordlen-4]=0;              //remove the suffix
        strcpy(word,mainpart);
        strcat(word,"eins");
        cout<<word<<endl;
        strcpy(word,mainpart);
        strcat(word,"zwei");
        cout<<word<<endl;
        .
        .
        .
    }
    else
    {
        cout<<"You didn't input the word by convention.";
    }
    return 0;
}


You can use vectors if you have very large words. I am not good in vectors yet so did it in arrays.
There are some bugs in the program though.
Like, if I enter a word "hellolue",
the output will be :
helleins
hellzwei ...

But this code will atleast give you some idea.
Last edited on
This should give you an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    int x = 0;
    std::string toread = "";
    std::string compare = "red"; //Declare the string we want to search for.
    std::cout << "Enter a line of text: ";
    getline(std::cin, toread); //Have the user enter a line of text.
    std::size_t f = toread.find(compare); //Search the string for the position of "red"
    std::cout << "red found at: " << f << "\n"; //f = Start position of string red.
    toread.replace(f,f+2,"one"); //Replace "red" with "one".
    std::cout << "Modified: " << toread << "\n"; //Output the modified string.
    return 0;
}


This will need two major modifications (can't do your whole assignment for you ;)):

-Continue searching until end of string. For now, it only will only find the first instance of red and replace it.

-Also search for yellow and blue and replace those appropriately.

Hope this gives you a good start. Post any more questions if you have them.
Topic archived. No new replies allowed.