Pages

Friday, November 3, 2017

Easy tutorial for C / C ++ - string 3 (strcpy(), strcmp() functions)

Easy tutorial for C / C ++ - string 3 (strcpy(), strcmp() functions)

strcpy() :

- 'strcpy ()' is a function that copies a string. The usage is as follows.

strcpy ("String variable to store", "Source to copy")

- Let's look at an example.

Example Code

#include <iostream>
#include <string.h>
using namespace std;

int main() {
     char st1[255] ;
     char st2[255] ;
     char st3[255] ;

     cout << "Enter a string. : " ;
     cin.get(st1,255) ;
     cout << " st1         = " << st1 << endl ;
     cout << endl ;

     cout << "After \'strcpy\' method : " << endl ;
     strcpy(st2, st1);
     cout << " st2         = " << st2 << endl ;
     cout << endl ;

     cout << strcpy(st3, "Hello !") << endl ;
     return 0;
}

- We have defined the string variables 'st1, st2, st3'. We used 'get ()' method to store the input string in 'st1' and print it.

- 'strcpy' was used to copy the string stored in 'st1' to 'st2'. And the contents of 'st2' will be printed.

- As in assigning variables, use 'strcpy' to copy and print. To copy and print. (An error occurs if you write code st3 = "Hello!")

results :

Enter a string. :

- You will be prompted to enter a string. Here "Hi everyone!" And press Enter.

Enter a string. : Hi everyone !
 st1         = Hi everyone !

After 'strcpy' method :
 st2         = Hi everyone !

Hello !

- The output of 'st1' and 'st2' are both "Hi everyone!" You can see that the copy is done well. And as a result of 'st3', "Hello!" is printed.

strcmp() :

- The 'strcmp ()' function is a function that compares the size of a string. Rather than comparing the length of a string, it replaces the character with ASCII code and compares the size of the code value before the string. Returns 0 or a number less than 0 or larger than 0 after string comparison.

How to use
strcmp ("string1", "string2")
'' String 1 "" <"string 2" return value <0
'' String 1 '' = 'string 2' return value = 0
'' String 1 "> return value if" string 2 "> 0
- Let's look at an example.

Example Code

#include <iostream>
#include <string.h>

using namespace std;
int main() {
     char st1[255] ;
     char st2[255] ;

     cout << "Enter a string for \'st1\' : " ;
     cin.get(st1,255) ;
     cin.ignore(255,'\n');

     cout << "Enter a string for \'st2\' : " ;
     cin.get(st2,255) ;

     if(strcmp(st1,st2) > 0){
       cout << "\'st1\' > \'st2\' " << endl ;
          } else if (strcmp(st1,st2) < 0){
       cout << "\'st1\' < \'st2\' " << endl ;
          } else {
            cout << "\'st1\' = \'st2\' " << endl ;
          }
     return 0;
}

- We defined the string variables 'st1, st2' and stored 'st1, st2' using the 'get ()' method.

- I wrote a sentence that compares the sizes of 'st1' and 'st2' using the if else statement and prints the result.

results :

Enter a string for 'st1' : abc
Enter a string for 'st2' : def
'st1' < 'st2'

- When you execute it, you will be prompted to enter a string in 'st1' and 'st2'. If you type 'abc' and 'def' here, 'st1' <'st2' will be output. This is because the ASCII code value of d is larger than a.

Enter a string for 'st1' : hello
Enter a string for 'st2' : hello
'st1' = 'st2'

- Now, let's enter the same string. It returns 0 in the same string and you will see the output of 'st1' = 'st2'.

No comments:

Post a Comment