strcpy cpp code example
Example 1: c++ strcpy_s
//When to use strcpy_s:
// Use strcpy_s to copy a const char[] array in read and write memory
//How to use strcpy_s:
//The location where the array is going to be copied to
char* ptrToArray = new char[sizeof(testArray)]
//The Array that gets copied To ptrToArray;
const char[] testArray = "Test Array";
strcpy_s(ptrToArray, sizeof(testArray), testArray);
//Modify the copied array
testArray[i] = 'A'
Example 2: strcpy
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);//str1 copies to str2
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
Example 3: strcmp c++
#include<stdio.h>
#include<string.h>
int main()
{
char char1[] = "coucou";
char char2[] = "coucou";
if( strcmp(char1, char2) == 0 )
printf("Strings are the same");
else
prinf("Strings are differentes");
return 0;
}
Example 4: strcmp c++
int strcmp ( const char * str1, const char * str2 );
// returning value | indicates
// <0 the first character that does not match has a lower value in ptr1 than in ptr2
// 0 the contents of both strings are equal
// >0 the first character that does not match has a greater value in ptr1 than in ptr2