Pages

Tuesday, August 15, 2017

C-style C++ String processing

This program will demonstrate use of C++ string processing using C-style string functions.

#include <iostream>
#include <cstring>

using namespace std;

int main () {
   int  len;
   char str1[10]="Hello", str2[10]="World", str[20];
   
   //String copy function: str1 into str
   strcpy( str, str1);
   cout << "Output of strcpy(str,str1): "<< str <<endl;

   //String concatenate: str1 and str2
   strcat( str1, str2);
   cout << "Output of strcat(str1,str2): "<< str1 <<endl;

   //Total lenghth of str1 after strcat()
   len = strlen(str1);
   cout << "Length of str1: " << len <<endl;

   return 0;
}

Output of Program 

Output of strcpy(str,str1): Hello
Output of strcat(str1,str2): HelloWorld
Length of str1: 10

No comments:

Post a Comment