-5

hii guys i need a serious help i m trying to write a code for finding anagrams in input sentence but when the if function is getting strcmp it stops and its not accepting the condition. any body know why is that happening Basically my code supposed to do two things one is taking a sentence from the user and making the words appear in the Backwoods order two Its need to take the whole sentence and look for anagrams ( anagram means that there is the same letters but in a different order for example this and shit are anagrams) thank you very much for your help :)

#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);
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
BIG.E
  • 1
  • 3
    Do you know the difference between C, C++ and C#? – Steve May 14 '17 at 19:47
  • 1
    Some people simply do not want to have their question answered. Things happen... –  May 14 '17 at 19:55
  • 1
    I am a student right now and I'm learning. sure you were learning programming once two and instead off being difficult you can try to help. Patience is the key my friend. But thank you anyway and have a great day – BIG.E May 14 '17 at 20:06
  • What makes you think that `strcmp` will detect if two strings are anagrams? All it does is compare the two strings... – rici May 14 '17 at 20:11
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the **shortest code necessary** to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](/help/mcve) – MD XF May 14 '17 at 20:32

1 Answers1

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;
}
shwkumar
  • 68
  • 6
  • First you declare two arrays of length 26 which would denote the 26 alphabets in the English language for the two arrays. That is, using these two arrays we'll keep a count of the alphabets present in both words. Then we'll iterate through both strings & increase the count of the letters present in the strings by doing an increment of the corresponding index. What I mean to say is, if we have 1st string as "arm" then for 'a' we'll have a[0]++ 'r' we'll have a[17]++ 'm' we'll have a[12]++ We'll do the same for the 2nd string & then compare both of them in the last loop. – shwkumar May 14 '17 at 21:36
  • There is an option of using gets but I don't know where the OP wants to use it so I left it out, but I'll add scanf("%[^\n]s" instead – shwkumar May 14 '17 at 22:06
  • I posted a solution only after testing but I had to change the solution because I wasn't able to post the code properly & I didn't pay attention to the slashes. I only wanted to help. I messed up a bit. As far as I know, the OP is not going to use a program to check for anagrams in a real world program. It must be an assignment. But thanks for pointing out my mistake. Correct coding is mandatory. I will make the proper edits – shwkumar May 15 '17 at 15:20
  • Not trying to be mean; just trying to help with correctness. Managing user input can be tricky in C! DV removed. One final note: it would be much better if you moved your discussion of the solution from the comments section into your posted answer. Comments aren't meant to be permanent, and sometimes may mysteriously disappear. It is also easier for future readers to find your discussion if it is in the actual answer. – ad absurdum May 15 '17 at 15:54
  • I removed my comments about earlier issues, but want to reiterate that this method of using, e.g., `second[b[c]-'a']` is not portable. It will quite likely work, but C places few restrictions on character encoding, and does not specify ASCII, or UTF-8, in particular. There are some real-world systems that use other encodings; for example, some old IBM systems use EBCDIC. In this encoding, the letters are not encoded in a contiguous sequence, so this method would fail. I have heard of these issues arising on systems in institutional settings. – ad absurdum May 15 '17 at 16:00
  • thank you all for the help i found the problem. have a great day you all :) – BIG.E May 16 '17 at 19:14