>first; cout<<"Enter last name: "; cin>>last; wholeName=first+" "+last; cout< Download presentation Presentation is loading. Please wait. Published byCharleen Ellis
Modified over 9 years ago
1
String in C++
2
Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; str3 = str1; // copy str1 into str3 cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 <<endl; //total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len <<endl; }
3
Example using build in functions of class string #include using namespace std; void main() { string first, last, title; string wholeName; string greeting= "Hello "; cout<<"Enter first name: "; cin>>first; cout<<"Enter last name: "; cin>>last; wholeName=first+" "+last; cout<<greeting<<wholeName<<'!'<<endl; cout<<"You have "<<(wholeName.length()-1)<<" letters in your name. "<<endl;
4
cout<<"Your initials are"<< first.at(0)<< last.at(0); cout<<"\n position of last "<<wholeName.find(last) ; cout<<endl; wholeName.insert(0,"Dr."); cout<<greeting<<wholeName<<'!'<<endl; wholeName.insert(9,"sami "); cout<<greeting<<wholeName<<'!'<<endl; wholeName.replace(9,7,"F."); cout<<greeting<<wholeName<<'!'<<endl; wholeName.erase(9,3); cout<<greeting<<wholeName<<'!'<<endl; title.assign(wholeName,0,4); cout<<greeting<<wholeName<<'!'<<endl; cout<<greeting<<title<<'!'<<endl; }
6
The C-Style Character String: Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character ‘\0’. char sname[ ] = “Ali Samer”; // 10 locs or char sname[ ] = {‘A’,’l’,’i’,’ ’,‘S’,’a’,’m’,’e’,’r’,’\0’} or char sname[10 ] = {‘A’,’l’,’i’,’ ’,‘S’,’a’,’m’,’e’,’r’,’\0’}
7
String String can be constant pointer that points to the string’s first character. char *sname = “Ali Samer”; Sname points to ‘A’ “Ali Samer’\0’” in Some where in memory
8
Example void main( ) { char firstName[] = "Ahmad"; char *lastName = "Omar"; cout<<"First Name: "<<firstName<<endl; cout<<"Last Name: "<<lastName<<endl; int i=0; cout<<"FirstName: "; while (firstName[i] != '\0') cout<<firstName[i++]; i=0; cout<<"\nLast Name: "; while (lastName[i] != '\0') cout<<lastName[i++]; }
9
Reading String char studentName[20]; cin>>studentName; cout<<studentName<<endl; Problem: read characters until the first white space.
10
Reading String solution: use cin.getline(array_name, array_size, delimiter) char studentName[20]; cin.getline(studentName,20,'\n'); cout<<studentName<<endl; Remark: Pointer to character string can not be readed
11
String Library cstring.h or string.h This library include set of built in functions for manipulating the strings. Like: 1- strcpy(s1, s2) s1 = s2 Copies string s2 into string s1. #include void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }
12
2- strncpy(s1, s2) s1[n] = s2[n] #include void main() { char str1[] = ” **********"; char *str2 = “ $$$$$$$$$$"; strncpy(str1,str2,5); cout<<str1<<endl; }
13
3- strcat(s1, s2) s1 = s1+s2 Concatenates string s2 onto the end of string s1. #include void main() { char str1[24] = ” Philadelphia"; char *str2 = “ University"; strcat(str1,str2); cout<<str1<<endl; }
14
4- strncat(s1, s2,n) s1 = s1+s2[n] #include void main() { char str1[24] = ” Philadelphia"; char *str2 = “ University of Jordan"; strncat(str1,str2,10); cout<<str1<<endl; }
15
5- strcmp(s1, s2) 0 if s1 = s2 -1 if s1 < s2 1 if s1 > s2 Symbols < … < numbers < … < capital letters < …. < smal letters. #include void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strcmp(str1,str2)) if (strcmp(str1,str2) == 1) cout "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; }
16
6- strncmp(s1, s2,n) 0 if s1[n] = s2[n] -1 if s1[n] < s2[n] 1 if s1[n] > s2[n] #include void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strncmp(str1,str2,1)) if (strcmp(str1,str2,1) == 1) cout "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; }
17
7- strlen(s) How many characters in s is a function that accepts a string, defined as an array of characters, and returns the number of characters in the string excluding null character #include void main() { char s1[] = "Ahamd Ali"; char *s2 = "Amman City"; cout<<s1<<" Consists of "<<strlen(s1)<<" Characters.\n"; cout<<s2<<" Consists of "<<strlen(s2)<<" Characters.\n"; }
Similar presentations © 2025 SlidePlayer.com. Inc. Log in
String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len.
Similar presentations
Presentation on theme: "String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len."— Presentation transcript:
Similar presentations
All rights reserved.