473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. const string name = "John Paul Jones";
  7.  
  8. int main() {
  9.  
  10.  
  11. int spacePos = name.find(' ');
  12. // int len = name.length();
  13. string firstName = name.substr(0, spacePos);
  14. string middleName = name.substr(spacePos + 1);
  15.  
  16.  
  17.  
  18. cout << firstName + ' ' << middleName << endl;
  19.  
  20. return 0;
  21.  
  22. }
Jan 31 '07 #1
5 3778
r035198x
13,262 8TB
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(spacePos + 1);



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

return 0;

}

Like this?
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. const string name = "John Paul Jones";
  6. int main() {
  7.  int spacePos = name.find(' ');
  8.  string firstName = name.substr(0, spacePos);
  9.  string temp = name.substr((spacePos + 1), name.length());
  10.  int nextSpacePos = temp.find(' ');
  11.  string lastName = temp.substr(nextSpacePos + 1, temp.length()-1);
  12.  string middleName = name.substr(spacePos + 1, nextSpacePos);
  13.  cout << firstName<<endl;
  14.  cout<<middleName<<endl;
  15.  cout<<lastName<< endl;
  16.  return 0;
  17. }
  18.  
Jan 31 '07 #2
Motoma
3,237 Expert 2GB
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(spacePos + 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.
Jan 31 '07 #3
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!!

Like this?
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. const string name = "John Paul Jones";
  6. int main() {
  7.  int spacePos = name.find(' ');
  8.  string firstName = name.substr(0, spacePos);
  9.  string temp = name.substr((spacePos + 1), name.length());
  10.  int nextSpacePos = temp.find(' ');
  11.  string lastName = temp.substr(nextSpacePos + 1, temp.length()-1);
  12.  string middleName = name.substr(spacePos + 1, nextSpacePos);
  13.  cout << firstName<<endl;
  14.  cout<<middleName<<endl;
  15.  cout<<lastName<< endl;
  16.  return 0;
  17. }
  18.  
Feb 1 '07 #4
Thank you! :)

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.
Feb 1 '07 #5
Motoma
3,237 Expert 2GB
Thank you! :)
Come back anytime you have a problem, we'll be happy to help out.
Feb 1 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: MIchel | last post by:
Hi Group, I want to reprint a part of a form if the length of the input is higher than a predetermed length. For instance, if someone posts a name, longer than 15 characters, I want to cut it...
5
by: jmaxsherkimer | last post by:
i had a basic question on plsql, if i have a variable as varchar2, containing some text like 'A12345', how do i write a function to make sure that the first char. is the letter 'A' or 'a' and the...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
7
by: Randy Yates | last post by:
OK, why doesn't this work: string EraseCommas(string &numericString) { UINT16_T commaPosition; commaPosition = numericString.find(","); while (commaPosition != string::npos); {
3
by: ypress | last post by:
Hi. I have a page that contains this simple function: <script language="javascript"> function openPop(url, name, w, h) { var features = ""; features += "scrollbars=no,"; features +=...
8
by: Josué Maldonado | last post by:
Hello List, I'm importing some data from Foxpro to Postgres, there is atable wich contains aprox 4.8 million rows and it size about 830MB. I uploaded it to Postgres using dbf2pg and worked fine,...
7
by: yawnmoth | last post by:
Say I have the following script: <?php $contents = file_get_contents('preg_test.txt'); echo preg_match("#(.*?)+ddddddddddddd#s",$contents) ? 'is equal' : 'is not equal'; ?> Here's...
7
by: markscottwright | last post by:
I'm using visual studio 8, and the following code is failing. For the life of me, I can't see what's wrong... void myTest(std::string const& in) { using namespace std; string::const_iterator...
5
by: DavidPr | last post by:
I'm using sessions now but I've just realized a potential problem. It's a small job board and the employers have to register and login to post ads, and the job seekers have to register, login and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.