Separate a string in two parts

Enter Your Name along with Roll No
Roll No NAME
For example
Mc1504029 Kuhshdil Kamal
fall 2016
BS40383 Nadeem
spring 2017
Output
Roll No NAME

Mc1504029 Kuhshdil Kamal
BS40383 Nadeem khan

This is possible please tell me

In the output the roll no and name is separate.
for example
output in this form

# | Roll no | Name of student| semester |Launching year
1. |Mc1504029| Kuhshdil Kamal| fall 2016 |2016
2. |BS40383 | Nadeem Khan |spring 2017|2017

also use structure for strings and function for inputs
Last edited on
Separate a string in two parts

Could you be more precise?

There are more ways to split strings, like for example:

 
std::string new_str(str1.begin(), str1.begin()+20);


For example this is a new string that has only the first 20 characters of the given one.
Last edited on
Yes. We tell you that it is indeed possible -- in more than one way.


However, what you have told does not appear to require such thing; in your example simply prints the input as is. You have to tell more and show what you have tried.
If you simply want to separate the roll number from the rest of the line (presumably the name, maybe in multiple words) then you could use the following.

(The [ ] in the output is just so that I can see what happens to leading and trailing blanks).

Not sure if this is what you are asking.

EDIT: You have just changed the question since I answered it! Please DON'T!
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;


//======================================================================


string trim( const string &s )                   // remove leading and trailing blanks
{
   int i = s.find_first_not_of( " " );   if ( i == string::npos ) return "";
   int j = s.find_last_not_of( " " );
   return s.substr( i, j - i + 1 );
}


//======================================================================


int main()
{
   string line;
   string rollNumber, name;
   cout << "Input roll number followed by name on the same line" << endl;

   getline( cin, line );          // read all input
   stringstream ss( line );       // convert to a stream
   ss >> rollNumber;              // take first string as rollNumber
   getline( ss, name );           // take rest as name (may be multiple words)
   name = trim( name );           // remove blanks at start and end

   cout << "Roll number is [" << rollNumber << "]" << endl;
   cout << "Name is [" << name << "]" << endl;
}


//====================================================================== 

Input roll number followed by name on the same line
Mc1504029 Kuhshdil Kamal
Roll number is [Mc1504029]
Name is [Kuhshdil Kamal]
Last edited on
Enter Your Name along with Roll No
Roll No NAME
For example
Mc1504029 Kuhshdil Kamal
fall 2016
BS40383 Nadeem
spring 2017
Output
Roll No NAME

Mc1504029 Kuhshdil Kamal
BS40383 Nadeem

This is possible please tell me

In the output the roll no and name is separate.
for example
output in this form

# Roll no Name of student semester year
1. Mc1504029 Kuhshdil Kamal fall 2016 2016
2. BS40383 Nadeem spring 2017 2017
Like so?

I've no idea why (or even if) you want the year repeated, so I haven't bothered to split it off.

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;


int main()
{
   string line1, line2;
   vector<string> students;

   while( true )
   {
      cout << "Input roll number and name (or # to stop): ";
      getline( cin, line1 );
      if ( line1[0] == '#' ) break;

      cout << "Input semester and year: ";
      getline( cin, line2 );

      students.push_back( line1 + " " + line2 );
   }

   cout << endl;
   for ( int i = 0; i < students.size(); i++ ) cout << i + 1 << ". " << students[i] << endl;
}

Input roll number and name (or # to stop): Mc1504029 Kuhshdil Kamal
Input semester and year: fall 2016
Input roll number and name (or # to stop): BS40303 Nadeem
Input semester and year: spring 2017
Input roll number and name (or # to stop): #

1. Mc1504029 Kuhshdil Kamal fall 2016
2. BS40303 Nadeem spring 2017
Last edited on
OK thanks for help
Topic archived. No new replies allowed.