How can I change word endings?

This is a discussion on How can I change word endings? within the C++ Programming forums, part of the General Programming Boards category; Hi! Sorry if my question sounds nave but is it possible to create a programme which would detect and change ...

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

    Question How can I change word endings?

    Hi!


    Sorry if my question sounds nave 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    2,590
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    you now have two references from which to study. I'd recommend looking at the find_last_of, substr, and replace functions.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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, 01: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, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21