Cont Assume that array int arr 5 has been declared and its first element is at

Cont assume that array int arr 5 has been declared

This preview shows page 30 - 44 out of 59 pages.

6.3 (Cont..)Assume that array int arr [5] has been declared and its first element is at location 100 in memory. Assume pointer int *ptrarrhas been initialized to the beginning address of arr, i.e.; 100. 100 102 104 106 108
Image of page 30
Image of page 31
6.3 (Cont..) Pointer arithmetic is meaningless unless performed on an array. // using strcpy and strncpy #include <iostream> #include <string> using namespace std; int main ( ) { char x [ ] = “Happy Birthday to you”; char y [ 25 ], z [15 ]; cout << ”The string in array x is:”<< x
Image of page 32
Example (Cont..) << ”\n The string in array y is: “ << strcpy(y, x) << ”\n”; strncpy (z, x, 14); // does not copy null character z [14] = ‘\0’; cout << ”The string in array z is: “ << z << endl; return 0; }
Image of page 33
Example (Cont..) The output of the above program is: The string in array x is: Happy Birthday to you The string in array y is: Happy Birthday to you The string in array z is: Happy Birthday
Image of page 34
6.3 (Cont..) Functions strcpy ( ) copies its second argument (a string) into its first argument which must be large enough to store the string and the terminating null character.
Image of page 35
Image of page 36
Example // using strcat and strncat #include <iostream> #include <string> using namespace std; int main ( ) { char s1[20 ]=”Happy ”; char s2 [ ] = “New Year”; char s3 [40] = “ “;
Image of page 37
Example (Cont..) cout << ”s1= “ << s1 << ”\n s2 = “ << s2; cout << ”\n strcat ( s1, s2) = “ << strcat (s1, s2); cout << ”\n strncat (s3, s1, 6) = “<< strncat (s3, s1, 6); cout << ”\n strcat (s3, s1,) = ” << strcat(s3, s1) << endl; return 0; }
Image of page 38
Image of page 39
6.3 (Cont..) Function strncat ( ) appends a specified number of characters from the second string to the first string. A terminating null character is appended to the result. Assuming that strcmp ( ) and strncmp ( ) return 1 when their argument are equal is a logic error. Both functions return 0 ( C++’s false value ) for equality. Therefore, when testing two strings for equality, the result of the strcmp ( ) or strncmp ( ) function should be compared with 0 to determine if the strings are equal.
Image of page 40
Example // using strcmp and strncmp #include <iostream> #include <string> using namespace std; int main ( ) { char *s1 = “Happy New Year”; char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”;
Image of page 41
Example (Cont..) cout << ”strcmp(s1, s2) = “ << strcmp (s1, s2) << "\n strcmp (s1, s3) = " << strcmp (s1, s3) << ”\n strcmp (s3, s1) = “ << strcmp (s3, s1); cout << ”\n\n strncmp (s1, s3, 6) = “ << strncmp (s1, s3, 6) << ”\n strncmp (s1, s3, 7 ) = “ << strncmp (s1,s3, 7) << ”\n strncmp (s3, s1, 7) = “ << strncmp (s3, s1, 7) <<endl;
Image of page 42
Image of page 43
Image of page 44

You've reached the end of your free preview.

Want to read all 59 pages?

  • Fall '18
  • Heidar Malki
  • Pointer, ASCII

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture