Input a string from user in the following format: Name_RollNo_GPA.
Now perform string tokenization operation on this string to separate the Roll no and GPA. Store each token in a separate row of 2d char array and then print that string. Plus store name in a string, roll no in integer and GPA in float. And also print them. You are not allowed to use atoi() and atof().
Note: You cannot call built in function of strtok()
#include <iostream>
#include <string>
using namespace std;
int main(){
cout<<"Input token: "; //e.g lucas_23_4.5
char c[2][20];
string name = "", r = "", g = "";
int roll;
float gpa;
cin>>c[0];
int i = 0, prev, count = 0;
for(int j = 0; j < 1; j++){
while(c[j][i] != '\0'){
if(c[j][i] == '_'){
if(count == 0)for(int k = 0; k < i; k++){
name += c[j][k];
prev = i;
if(k + 1 == i) count++;
}
else if(count == 1) for(int k = prev + 1; k < i; k++){
r += c[j][k];
prev = i;
if(k + 1 == i){
count++;
i++;
break;
};
}
}
if(count == 2) g += c[j][i];
i++;
}
}
roll = stoi(r);
gpa = stof(g);
cout<<c[0]<<endl;
cout<<name<<endl;
cout<<roll<<endl;
cout<<gpa<<endl;
return 0;
}
Comments
Leave a comment