Page 299 - Computer_Science_F5
P. 299

Computer Science  #include <iostream>

                        #include <cstring>
                        using namespace std;
                        int main()
                        {
          FOR ONLINE READING ONLY
                        char string1[]=”Hujambo”;
                        char string2[]=”Mwananzengo”;
                        strcat(string1, string2);
                        cout << “The result of strcat is : “ <<string1 << ‘\n’;
                        }

            Output: The value of string2 (Mwananzengo) will be concatenated to string1
                      (Hujambo) and string2 value will be printed as shown in Figure 4.66.








                                 Figure 4.68: Output of string concatenation



           strlen( )
           The  following Program Example  4.38 shows the  use of the  string function  for
           calculating length of a string.

                   Program Example 4.38:

            Using strlen( ) string function


            Taking the string s1 in strlen(s1), this string function of C++ will calculate and
            return the length of string s1.

                      #include <iostream>
                       #include <cstring>
                       using namespace std;

                       int main()
                       {
                       char string1[]=”Mzalendo madhubuti wa nchi yake”;
                       cout << “The result of strlen is : “ <<strlen(string1)<< ‘\n’;

                       }




                                                 290
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   290                                                     23/07/2024   12:34
   294   295   296   297   298   299   300   301   302   303   304