1 / 13

String in C++

String in C++. String. Series of characters enclosed in double quotes. “Philadelphia University” String can be array of characters ends with null character ‘’. char sname[ ] = “Ali Samer”; // 10 locs or char sname[ ] = {‘A’,’l’,’i’,’ ’,‘S’,’a’,’m’,’e’,’r’,’’}. String.

jock
Télécharger la présentation

String in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String in C++

  2. 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’}

  3. 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

  4. 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++]; }

  5. Reading String char studentName[20]; cin>>studentName; cout<<studentName<<endl; Problem: read characters until the first white space.

  6. 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

  7. String Librarycstring.h or string.h This library include set of built in functions for manipulating the strings. Like: 1- strcpy(s1, s2)  s1 = s2 #include <iostream.h> #include <cstring> void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }

  8. 2- strncpy(s1, s2)  s1[n] = s2[n] #include <iostream.h> #include <cstring> void main() { char str1[] = ”**********"; char *str2 = “$$$$$$$$$$"; strncpy(str1,str2,5); cout<<str1<<endl; }

  9. 3- strcat(s1, s2)  s1 = s1+s2 #include <iostream.h> #include <cstring> void main() { char str1[24] = ”Philadelphia"; char *str2 = “University"; strcat(str1,str2); cout<<str1<<endl; }

  10. 4- strncat(s1, s2,n)  s1 = s1+s2[n] #include <iostream.h> #include <cstring> void main() { char str1[24] = ”Philadelphia"; char *str2 = “University of Jordan"; strncat(str1,str2,10); cout<<str1<<endl; }

  11. Symbols < … < numbers < … < capital letters < …. < smal letters. 5- strcmp(s1, s2)  0 if s1 = s2  -1 if s1 < s2  1 if s1 > s2 #include <iostream.h> #include <cstring> 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<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; }

  12. #include <iostream.h> #include <cstring> 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<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; else cout<<str1<<" = "<<str2<<endl; } 6- strncmp(s1, s2,n)  0 if s1[n] = s2[n]  -1 if s1[n] < s2[n]  1 if s1[n] > s2[n]

  13. #include <iostream.h> #include <cstring> 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"; } 7- strlen(s)  How many characters in s

More Related