-4

I have a string which is having student roll number in it and its name like [33 john smith]. I need to get the value of roll number in a variable int id; and rest of string "john smith" in string name;

Can you please help me to get this information from string data = "33 john smith"; into int id=33; and string name ="john smith";?

Shahjahan KK
  • 91
  • 1
  • 3
  • 12
  • 1
    Why did you tag 3 languages ? Yes, it's doable but please show us what you have tried and the problem facing in it. – Mahesh Sep 13 '13 at 14:00
  • If you need a C++ answer, why did you tag this C and *Java* ? – Borgleader Sep 13 '13 at 14:00
  • What have you tried? Do you want to do it in `C/C++` or `Java`? Please update your tags accordingly? – PM 77-1 Sep 13 '13 at 14:00
  • 1
    Do you know **any** of these languages? It's very basic programming and (if necessary) can be done without regular expressions. – PM 77-1 Sep 13 '13 at 14:01
  • possible duplicate of [Splitting a string in C++](http://stackoverflow.com/questions/236129/splitting-a-string-in-c) – Manu343726 Sep 13 '13 at 14:02
  • You tag with [c], [c++], and [java], but then show what appears to be [objective-c] in the body. WUT – Cole Tobin Sep 13 '13 at 14:02
  • @ShahjahanKK SO is not the right place to get code handed to you. Please show some effort in solving this problem yourself, and ask for help if you are stuck. – nikolas Sep 13 '13 at 14:10

1 Answers1

1

Could use std::stringstream to archive what you want:

#include <sstream>
#include <string>

std::string s("33 john smith");
std::stringstream ss(s);

int id;  
std::string first_name, surname;
ss >> id >> first_name >> surname;
std::string data = first_name + " " + surname;
billz
  • 44,644
  • 9
  • 83
  • 100