20 likes | 159 Views
Session-8 CSIT 121 Spring 2006. Chapter 3 continued Text string processing (Functions to search for a sub-string, count the length of names and compare two strings) Example demo program Lesson 3-5 Exercises 1,2,3
E N D
Session-8 CSIT 121 Spring 2006 • Chapter 3 continued • Text string processing (Functions to search for a sub-string, count the length of names and compare two strings) • Example demo program • Lesson 3-5 Exercises 1,2,3 • Lab sheet : Extend Exercise 3 so that your program searches for a pattern “ob” in your full name and reports “pattern found” or “not found” on the screen
Demo Program for Strings • void main () • { • string myname, yourname; • int mylength, yourlength; • int pos; • cout<<"Enter first user's full name"; • getline(cin,myname); • cout<<"Enter second user's full name"; • getline(cin,yourname); • mylength = int (myname.length()); • yourlength = int (yourname.length()); • cout<<"My name is "<<mylength<<" characters long"<<endl; • cout<<"Your name is "<<yourlength<<" characters long"<<endl; • if (myname>yourname) cout<<"My name is longer"<<endl; • else cout<<"Your name is longer"<<endl; • pos = myname.find('Z'); • if (pos != -1) cout<<"Zub occurs at position "<<pos<<endl; cout<<myname.substr(5,6)<<endl; • }