比较C中两个字符串的排列

时间:2022-04-24 12:19:07

I've just started out with learning C-basics and tried solving this problem where, we have to check if two strings are equal provided any permutation. You may refer to this link: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/

我刚开始学习C-basics并尝试解决这个问题,我们必须检查两个字符串是否相等,只要有任何排列。您可以参考以下链接:https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/

I just wanted to get some solutions on how can I improve my code which gives the output as only 'NO':

我只想得到一些解决方案,我如何改进我的代码,使输出只为'NO':

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    int i, j, k, m, n, o, p;
    char a[100000], b[100000], *c, *d;
    scanf("%d", &i);

    for (j = 0; j < i; j++)
    {
        scanf("%s %s", a, b);
    }
    for (k = 0; a[k] != '\0'; k++)
    {
        n = rand() % k;
    }
    for (m = 0; b[m] != '\0'; m++)
    {
        o = rand() % m;
    }
    for (p = 0; p < j; p++)
    {
        if (a[n] == b[o])
        {
            printf("YES");
        }
        else
        {
            printf("NO");
        }
    }
    return 0;
}

Thanks for the help!

谢谢您的帮助!

1 个解决方案

#1


0  

For compare two string use of strcmp() :

比较两个字符串使用strcmp():

int strcmp(const char *str1, const char *str2)

Parameters :

  1. str1 − This is the first string to be compared.
  2. str1 - 这是要比较的第一个字符串。

  3. str2 − This is the second string to be compared.
  4. str2 - 这是要比较的第二个字符串。

Return Value :

返回值:

This function return values that are as follows :

此函数返回如下值:

  • if Return value < 0 then it indicates str1 is less than str2.
  • 如果返回值<0则表示str1小于str2。

  • if Return value > 0 then it indicates str2 is less than str1.
  • 如果返回值> 0则表示str2小于str1。

  • if Return value = 0 then it indicates str1 is equal to str2.
  • 如果返回值= 0则表示str1等于str2。

Example :

#include <stdio.h>
#include <string.h>

int main () {
   char str1[15];
   char str2[15];
   int ret;


   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcmp(str1, str2);

   if(ret < 0) {
      printf("str1 is less than str2");
   } else if(ret > 0) {
      printf("str2 is less than str1");
   } else {
      printf("str1 is equal to str2");
   }

   return(0);
}

#1


0  

For compare two string use of strcmp() :

比较两个字符串使用strcmp():

int strcmp(const char *str1, const char *str2)

Parameters :

  1. str1 − This is the first string to be compared.
  2. str1 - 这是要比较的第一个字符串。

  3. str2 − This is the second string to be compared.
  4. str2 - 这是要比较的第二个字符串。

Return Value :

返回值:

This function return values that are as follows :

此函数返回如下值:

  • if Return value < 0 then it indicates str1 is less than str2.
  • 如果返回值<0则表示str1小于str2。

  • if Return value > 0 then it indicates str2 is less than str1.
  • 如果返回值> 0则表示str2小于str1。

  • if Return value = 0 then it indicates str1 is equal to str2.
  • 如果返回值= 0则表示str1等于str2。

Example :

#include <stdio.h>
#include <string.h>

int main () {
   char str1[15];
   char str2[15];
   int ret;


   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcmp(str1, str2);

   if(ret < 0) {
      printf("str1 is less than str2");
   } else if(ret > 0) {
      printf("str2 is less than str1");
   } else {
      printf("str1 is equal to str2");
   }

   return(0);
}