Thread: How can I change word endings?

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    5

    Question How can I change word endings?

    Hi!


    Sorry if my question sounds naïve but is it possible to create a programme which would detect and change word endings? I would like to write a simple verb conjugator, i.e.:


    If you enter a word ending with AAA then I would like the programme to show, say, six modified words:



    1. Word with the replaced ending, for example QWERTYAAA changes into QWERTYBBB
    2. QWERTYAAA changes into QWERTYCCC
    3. QWERTYAAA changes into QWERTYDDD



    etc.


    Is it possible to do it? If so, how? Your help is very much appreciated!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes it is possible

    start by writing a program that reads a string and writes it back to user as it is.
    post your code and continue from there.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    Thank you so much for your help!

    This is my code:

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	char str[50];
    	
    	cout << "Please enter your word: ";
    	cin >> str;
    	cout << "This is your word: ";
    	cout << str;
    
    
    	
    	return 0;
    }
    What should I do now?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Beginner++ View Post
    What should I do now?
    1. stop using char arrays.
    2. use std::string by including the <string> header
    3. study the std::string class, and learn to use its member functions to look for a suffix and replace it with another.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    Quote Originally Posted by Elkvis View Post
    1. stop using char arrays.
    2. use std::string by including the <string> header
    Thank you for your help. I've already changed that and now my code is:

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        std::string word;
        
        cout << "Please enter the word: ";
        cin >> word;
        cout << "This is your word: ";
        cout << word;
        
        return 0;
    }
    Quote Originally Posted by Elkvis View Post
    3. study the std::string class, and learn to use its member functions to look for a suffix and replace it with another.
    I don't know, however, what member functions I should use to detect the endings and change them. Could you please provide me with more guidance?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you now have two references from which to study. I'd recommend looking at the find_last_of, substr, and replace functions.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    Probably I'm doing something wrong but when I enter string substr (size_t pos = 3, size_t len = 3) const; into my code there's an error stating that "non-member function cannot have cv-qualifier".

    It seems to me, at least from the description, that I can't use this to find and change the ending. How can I know the position of the first character? I want the program to check if the word ends with, let's say, aaa. If it does, then I want the program to change it into bbb. Finding the first "aaa" in the word would be useless as the word could possibly begin and end with "aaa" (aaacccaaa). Also I don't know the length of the word so I am not sure if specifying the exact position of suffix in size_t pos=x would solve the problem. I am confused.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    Quote Originally Posted by Elkvis View Post
    you now have two references from which to study. I'd recommend looking at the find_last_of, substr, and replace functions.
    Thank you, I'll read about them.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Beginner++
    Probably I'm doing something wrong but when I enter string substr (size_t pos = 3, size_t len = 3) const; into my code there's an error stating that "non-member function cannot have cv-qualifier".
    Why would you enter that into your code? It provides you with a declaration of the member function to refer to in the text.

    Quote Originally Posted by Beginner++
    It seems to me, at least from the description, that I can't use this to find and change the ending.
    That is true (somewhat: I think you can use it to find the ending, but it probably wouldn't be the right tool for the job), but you are not limited to using only one function to solve a problem.

    Quote Originally Posted by Beginner++
    How can I know the position of the first character?
    Look at the other functions available.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Replies: 4
    Last Post: 03-02-2011, 10:35 AM
  3. Change word in a file
    By thescratchy in forum C Programming
    Replies: 8
    Last Post: 03-18-2010, 12:38 PM
  4. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  5. Mac - Default Lines Endings - fgets() - no worky
    By Dino in forum C Programming
    Replies: 6
    Last Post: 01-30-2008, 11:59 PM