strcpy, strlen, strcmp, strcat : string function « Data Types « C++ Tutorial






#include <iostream> 
#include <cstdio> 
#include <cstring> 
using namespace std; 
 
int main() 
{ 
  char s1[80], s2[80]; 
 
  strcpy(s1, "C++"); 
  strcpy(s2, " is power programming."); 
 
  cout << "lengths: " << strlen(s1); 
  cout << ' ' << strlen(s2) << '\n'; 
 
  if(!strcmp(s1, s2))  
     cout << "The strings are equal\n"; 
  else cout << "not equal\n"; 
   
  strcat(s1, s2); 
  cout << s1 << '\n'; 
 
  strcpy(s2, s1); 
  cout << s1 << " and " << s2 << "\n"; 
 
  if(!strcmp(s1, s2)) 
    cout << "s1 and s2 are now the same.\n"; 
 
  return 0; 
}
lengths: 3 22
not equal
C++ is power programming.
C++ is power programming. and C++ is power programming.
s1 and s2 are now the same.








2.24.string function
2.24.1.Using strcpy()
2.24.2.Using strncpy()
2.24.3.strcpy, strlen, strcmp, strcat
2.24.4.Convert a string to uppercase.
2.24.5.Using strcat and strncat.
2.24.6.Using strcmp and strncmp
2.24.7.Using strtok
2.24.8.Using strlen