1
내 프로그램은 사용자로부터 두 개의 입력을 받아서 지금까지 서로의 아나그램인지 알아 냈습니다. 지금까지 입력을 받았고 알파벳순으로 정렬되었지만 확실하지 않았습니다 그들은 분명히 문자열 내 코드가 아닙니다을 heres 동일 또는 경우 == 문자열을 인쇄를 비교하는 방법 당신은 strcmp()
를 사용할 필요가두 개의 입력이 서로의 아나그램이라면 인쇄하십시오.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort_string(char*);
int main()
{
char string[100];
char strings[100];
printf("\nThis program will ask you for 2 words and compare them to see if they are anagrams of each other\n\n");
printf("Enter first word\n");
gets(string);
sort_string(string);
/*commented out for testing of function*/
/*printf("%s\n", string);*/
printf("Enter second word for comparison\n");
gets(strings);
sort_string(strings);
/*commented out for testing of function*/
/*printf("%s\n", strings);*/
if (sizeof string==sizeof strings)
printf("\nThe two words ARE anagrams of each other.\n");
else
printf("\nThe two words are NOT anagrams of each other.\n");
printf("\nThank You %d %d\n\n",sizeof string, sizeoof strings);
return 0;
}
/*function to sort in alphabetical order to be used for comparison*/
void sort_string(char *s)
{
int c, d = 0, length;
char *pointer, *result, ch;
length = strlen(s);
result = (char*)malloc(length+1);
pointer = s;
for (ch = 'a' ; ch <= 'z' ; ch++)
{
for (c = 0 ; c < length ; c++)
{
if (*pointer == ch)
{
*(result+d) = *pointer;
d++;
}
pointer++;
}
pointer = s;
}
*(result+d) = '\0';
strcpy(s, result);
free(result);
}
'string1'과'string2'는'string'과'strings'보다 훨씬 더 나은 이름이 될 것입니다 –