các tính năng của hàm string

PHOTO EMBED

Thu Aug 19 2021 02:44:08 GMT+0000 (Coordinated Universal Time)

Saved by @_Ryan_ #c

#include<stdio.h>
#include<stdbool.h>
#include<conio.h>
#include<math.h>
#include<string.h>

void main()
{
   int choice;
   int choose;
   char str1[20], str2[20];

   printf("enter string 1: ");
   gets(str1);
   printf("enter string 2: ");
   gets(str2);


  do
  {
   printf("\n");
   printf("==========================\n");
   printf("= 1. compare two strings =\n");
   printf("= 2. copy string         =\n");
   printf("= 3. concatenation       =\n");
   printf("= 4. exit                =\n");
   printf("==========================\n");

   printf("choice: ");
   scanf("%d", &choice);
   int result;

   switch(choice)
   {
   case 1:
    result = strcmp(str1, str2);
    if(result == 0)
    {
        printf("two strings is equal");
    }
    if (result > 0)
    {
       printf("string 1 > string 2");
    }
    else
    {
        printf("string 1 < string 2");
    }
    break;
   case 2:
       printf("you wants copy string 1 into 2(input: 1)or 2 into 1(input 2) ?\n");
       printf("choose: ");
       scanf("%d", &choose);
        if(choose == 2)
        {
            strcpy(str1, str2);
            printf("string 2: ");
            puts(str2);
            printf("string 1: ");
            puts(str1);

        }
        else
        {
            strcpy(str2, str1);
            printf("string 2: ");
            puts(str2);
            printf("string 1: ");
            puts(str1);
        }
        break;
   case 3:
        printf("you wants concatenation string 1 into 2(input: 1)or 2 into 1(input 2) ?\n");
        printf("choose: ");
         scanf("%d", &choose);
        if(choose == 2)
        {
            strcat(str1, str2);
            printf("string 1:");
            puts(str1);
        }
        else
        {
            strcat(str2, str1);
            printf ("string 2: ");
            puts(str2);

        }
    break;
   default :
       exit(0);
    break;
   }

  }while( choice <= 3);






    getch();

}
content_copyCOPY