okay, so i'm totally new to this.
the purpose of my assignment is to compare two strings and to see if they're anagrams
my problem is that when i run it, it compiles okay, but then i'd get a message from windows, saying that it has stopped working.
er, yeah. i've no clue whats wrong, so any help would be appreciated.
thanks!

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

char *string1[100000], *string2[100000];
int a, b, c, count1[27], count2[27], m, no;

int main () {
    
    for (m=0; m<=26; m++){
        count1[m]=0;
        count2[m]=0;
        }
        
    gets(*string1);
    gets(*string2);
    
    for (a=0; a<=strlen(*string1); a++){
        if (isalpha(*string1[a]))
        count1[*string1[a]]++;
        }
    
    for (b=0; b<=strlen(*string2); b++){
        if (isalpha(*string2[b]))
        count2[*string2[b]]--;
        }
        
    for (c=0; c<=26; c++){
        if (count1[c]+count2[c]!=0)
        no++;
        }
        
    if (no==0)
    printf("YES\n");
    else
    printf("NO\n");
    
    system("pause");
    return 0;
}

Recommended Answers

All 4 Replies

Instead of intilaizing to zero use static before int.

LIKE

static int g[2];

static int Var[2];

For starters, instead of

char *string1[100000], *string2[100000];

you'll be doing much better of with something like

#define MAX_STRING_LEN 123 // define some reasonable string length
                           // 100000 really is not in that category, I think
char string1[MAX_STRING_LEN] = "";
char string2[MAX_STRING_LEN] = "";

Then after you've gotten the user's input for both strings,
sort the characters in the strings (i.e. "acb" becomes "abc"),
and compare the two sorted strings using e.g. strcmp() or strcmpi().
If they compare equal, it's an anagram (maybe given with the exception that they don't compare equal unsorted)
About getting the user's input, instead of gets(), see
http://www.gidnetwork.com/b-56.html

char *string1[100000], *string2[100000];  // You just created 
                          // an arrayof 100000 POINTERs not a 
                          // string of 100000 characters

int a, b, c, count1[27], count2[27], m, no;

int main () {
    
    for (m=0; m<=26; m++){
        count1[m]=0;         // Now you set all the POINTERs to 0
        count2[m]=0;
        }
        
    gets(*string1);          // read in something into a buffer 
                             // pointed to by the address of 
                             // STRING1.  On other words, 
                             // somewhere in Never-Never Land
    gets(*string2);

What you want, as mitrmkar explains, is to create an array of chars, not and array of pointers. He also is correct -- 100000 is much too large. Unless you are planning on typing in a novel.

Also, see this about gets()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.