2013-03-29 137 views
1

我的程序从用户处获取两个输入,并确定它们是否是对方的字典,但是我得到了输入并按照字母顺序排序了它们但不确定如何比较它们打印出来,如果他们是相同的或不继承人我的代码显然是串==字符串是不正确如果两个输入是互不相关的字符,则打印输出

#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); 
} 
+1

'string1'和'string2'会比'string'和'strings'好得多的名字 –

回答

相关问题