尝试使用strcmp in if函数来计算句子中的字谜

时间:2017-05-14 19:46:04

标签: c strcmp

伙计们,我需要一个认真的帮助 我试图编写一个代码来输入输入句子中的字谜 但是当if函数获得strcmp时它会停止并且不接受条件。任何人都知道为什么会这样 基本上我的代码应该做两件事,一个是从用户那里拿一个句子并使这些单词出现在Backwoods中。它需要整个句子并寻找anagrams(anagram意味着有相同的字母但是在不同的例如这个和狗屎是anagrams)非常感谢你的帮助:)

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

void main()
{
int index_for_word_start, words_num = 1,amount_of_letters;
int i, j, k;
char inpot_Sentence[1001], temp_letters;
char **words,**sorting_words;
int counter = 0,counter_max_for_anegram=0;



printf_s("Please enter the sentence, and then press Enter:\n");
gets(inpot_Sentence);                                  
                   /////////////////////////////makeing the sentence backwards///////////////////////




for (i = 0; inpot_Sentence[i] != '\0'; i++)                 //loop for counting how many words(it will be use to know how many pointer we need)
{
    if (inpot_Sentence[i] == ' ')
    {
        words_num++;
    }
}
words = (char **)malloc(sizeof(char *)*words_num);          //malloc for pointers that point on the pointer of the word
index_for_word_start = 0;
for (j = 0; j<words_num; j++)
{
    for (i = index_for_word_start; inpot_Sentence[i] != ' '; i++)
    {
        if (!inpot_Sentence[i])                                       //if the user didnt put any word(break)
        {
            break;
        }
    }
    words[j] = (char*)malloc(sizeof(char)*(i - index_for_word_start + 1));                  //malloc of pointers that point on each word
    strncpy_s(words[j], i - index_for_word_start+1, &inpot_Sentence[index_for_word_start], i - index_for_word_start);     //copy the words from inpot sentence to array
    words[j][i - index_for_word_start] = 0;                                                 //puts '\0' after the word copy ends
    index_for_word_start = i + 1;

}
printf_s("\nThe reverse sentence is:\n");
for (i = words_num - 1; i >= 0; i--)                                   //print the words in backwards Sequence
{
    printf("%s ", words[i]);
}
putchar('\n');
i = 0;
                                   /////////////////////anegrams check///////////////////////


for (j = 0; j < words_num; j++)                             //loops that Arrange the array by haski value
{
    amount_of_letters = strlen(words[j]);
    for ( i = 0; i < amount_of_letters; i++)
    {
        for (k = 0; k < amount_of_letters; k++)
        {
            if (words[j][i]<words[j][k])
            {
                temp_letters = words[j][i];
                words[j][i] = words[j][k];
                words[j][k] = temp_letters;
            }
        }

    }
    printf_s("this is words %s\n", words[j]);
}i = 0;
for ( j = 0; j < words_num-1; j++)
{
    for ( i = 0; i < words_num-1; i++)
    {
        if (!strcmp(words[j],words[i]) && (i!=j) && (strcmp(words[j],"\0")))
        {
            counter++;
            words[i] = 0;

        }
        else
        {
            break;
        }
    }
    if (counter>counter_max_for_anegram)
    {
        counter_max_for_anegram = counter;
    }
    counter = 0;
}
printf_s("%d\n", counter_max_for_anegram);


for ( j = 0; j < words_num; j++)
{
    free(words[j]);
}
free(words);
}

1 个答案:

答案 0 :(得分:1)

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

int check_anagram(char[],char[]);

int main()
{
    char a[100],b[100];
    int flag;
    puts("Enter the first string");
    fgets(a,100,stdin);
    a[strcspn(a, "\r\n")] = '\0';
    puts("Enter the second string");
    fgets(b,100,stdin);
    b[strcspn(b, "\r\n")] = '\0';
    flag=check_anagram(a,b);
    if(flag)
        printf("%s and %s are anagrams",a,b);
    else 
        printf("%s and %s are not anagrams",a,b);
}

int check_anagram(char a[], char b[])
{
    int first[26]={0},second[26]={0},c=0;
    while(a[c]!='\0')
    {
        first[a[c]-'a']++;
        c++;
    }
    c=0;
    while(b[c]!='\0')
    {
        second[b[c]-'a']++;
        c++;
    }
    for(c=0;c<26;c++)
    {
        if(first[c]!=second[c])
            return 0;
    }
    return 1;
}