Question about: name.substr(), and name.find()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rafael Olarte
    New Member
    • Jan 2007
    • 14

    Question about: name.substr(), and name.find()

    Dear friends,

    I appreciate any feedback on this problem.

    I am trying to be able to produce an output with first, middle, and last name using a const string value. Therefore, no matter what name is use on the const string value, it should always display the full name.

    So far, I have been able to get the first name correctly. However, when I try to get the middle name, it wraps the last name as well...


    Thanks for your any valuable advise.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const string name = "John Paul Jones";
    
    int main() {
    
    
    int spacePos = name.find(' ');
    // int len = name.length();
    string firstName = name.substr(0, spacePos);
    string middleName = name.substr(spacePos + 1);
    
    
    
    cout << firstName + ' ' << middleName << endl;
    
    return 0;
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Rafael Olarte
    Dear friends,

    I appreciate any feedback on this problem.

    I am trying to be able to produce an output with first, middle, and last name using a const string value. Therefore, no matter what name is use on the const string value, it should always display the full name.

    So far, I have been able to get the first name correctly. However, when I try to get the middle name, it wraps the last name as well...


    Thanks for your any valuable advise.

    #include <iostream>
    #include <string>

    using namespace std;

    const string name = "John Paul Jones";

    int main() {


    int spacePos = name.find(' ');
    // int len = name.length();
    string firstName = name.substr(0, spacePos);
    string middleName = name.substr(spa cePos + 1);



    cout << firstName + ' ' << middleName << endl;

    return 0;

    }

    Like this?
    Code:
     
    #include <iostream>
    #include <string>
    using namespace std;
    const string name = "John Paul Jones";
    int main() {
     int spacePos = name.find(' ');
     string firstName = name.substr(0, spacePos);
     string temp = name.substr((spacePos + 1), name.length());
     int nextSpacePos = temp.find(' ');
     string lastName = temp.substr(nextSpacePos + 1, temp.length()-1);
     string middleName = name.substr(spacePos + 1, nextSpacePos);
     cout << firstName<<endl;
     cout<<middleName<<endl;
     cout<<lastName<< endl;
     return 0;
    }

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by Rafael Olarte
      Dear friends,

      I appreciate any feedback on this problem.

      I am trying to be able to produce an output with first, middle, and last name using a const string value. Therefore, no matter what name is use on the const string value, it should always display the full name.

      So far, I have been able to get the first name correctly. However, when I try to get the middle name, it wraps the last name as well...


      Thanks for your any valuable advise.

      #include <iostream>
      #include <string>

      using namespace std;

      const string name = "John Paul Jones";

      int main() {


      int spacePos = name.find(' ');
      // int len = name.length();
      string firstName = name.substr(0, spacePos);
      string middleName = name.substr(spa cePos + 1);



      cout << firstName + ' ' << middleName << endl;

      return 0;

      }
      The problem is you never searched for the second space. Right now middleName is everything from the first space till the end of the line.

      Comment

      • Rafael Olarte
        New Member
        • Jan 2007
        • 14

        #4
        THANK YOU VERY MUCH. OWESOME!!! Now, I really understand how substitution works in these type of algorithm.

        By any change is there any book, or any source where I could get some of these problems for me to practise?

        Have a great day!!

        Originally posted by r035198x
        Like this?
        Code:
         
        #include <iostream>
        #include <string>
        using namespace std;
        const string name = "John Paul Jones";
        int main() {
         int spacePos = name.find(' ');
         string firstName = name.substr(0, spacePos);
         string temp = name.substr((spacePos + 1), name.length());
         int nextSpacePos = temp.find(' ');
         string lastName = temp.substr(nextSpacePos + 1, temp.length()-1);
         string middleName = name.substr(spacePos + 1, nextSpacePos);
         cout << firstName<<endl;
         cout<<middleName<<endl;
         cout<<lastName<< endl;
         return 0;
        }

        Comment

        • Rafael Olarte
          New Member
          • Jan 2007
          • 14

          #5
          Thank you! :)

          Originally posted by Motoma
          The problem is you never searched for the second space. Right now middleName is everything from the first space till the end of the line.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by Rafael Olarte
            Thank you! :)
            Come back anytime you have a problem, we'll be happy to help out.

            Comment

            Working...