“0000#7/29/1971#CD#chris@yahoo.com#Amr#99999.99#”The first substring is the person’s id; the second one is the person’s birthday; the third one is the person’s account type; the fourth one is the person’s email address; the fifth one is the person’s login password; the sixth one is the person’s current balance.
#include<iostream> using namespace std; #include<string> /* or <string.h> */ string parse(string s, int i) { string t=""; int counter=0; bool start=false; for(int j=0;j<s.length();j++) { if(s[j]=='#') counter++; if(!start&&counter==i-1) start=true; if(counter==i) break; if(start&&s[j]!='#') t+=s[j]; } return t; }However, if you know C++ string class very well, you probably know you can take advantage of some of the functions it provides such as find(). Here is another version of the function that utilizes C++ string functions:
#include<iostream> using namespace std; #include<string> /* or <string.h> */ string parse(string s, int i) { if(i<1) return ""; int d, second, first, pos; pos=first=0; for(d=0;d<i-1;d++) { first=s.find('#',pos); if(first==string::npos) return ""; pos=first+1; } second=s.find('#',pos); if(first==0) first=-1; return s.substr(first+1,second-first-1); }◀ Random Number Generator▶ Conversion Between char* and string