Presentation is loading. Please wait.

Presentation is loading. Please wait.

String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.

Similar presentations


Presentation on theme: "String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character."— 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: "<

5 Reading String char studentName[20]; cin>>studentName; cout<

6 Reading String solution: use cin.getline(array_name, array_size, delimiter) char studentName[20]; cin.getline(studentName,20,'\n'); cout<

7 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 #include void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<

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

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

10 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<

11 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 "<

12 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 "<

13 7- strlen(s)  How many characters in s #include void main() { char s1[] = "Ahamd Ali"; char *s2 = "Amman City"; cout<


Download ppt "String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character."

Similar presentations


Ads by Google