anagrams

This is a discussion on anagrams within the C Programming forums, part of the General Programming Boards category; i'm trying to make an anagram program, but i'm stuck on what to do within my setLetters function Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    19

    anagrams

    i'm trying to make an anagram program, but i'm stuck on what to do within my setLetters function
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define MAX 100
    //void initialize(int intarray[]);//initializes strings probably
    //char *gets_s(char s[], int num);, terminates a string with \0
    void getString(char string1storage[], char string2storage[]);
    void setLetters(char string1storage[], int intarray[]);
    void main(void)
    {//strings need2be initialized with anagram first?
    	
    	char string1storage[MAX], string2storage[MAX];//stores anagrams
    	int intarray[MAX] = {0};
    	//initialize(); //maybe call him after getstring?
    	getString(string1storage, string2storage);
    	setLetters(string1storage, intarray);
    }
    
    /*void initialize(intarray[])
    {
    	intarray[]={0};
    }*/
    
    void getString(char string1storage[], char string2storage[])
    {	
    	printf("Please enter a word or phrase");
    	gets_s(string1storage, MAX - 1);
    	printf("Please enter a second word or phrase");
    	gets_s(string2storage, MAX - 1);
    }
    
    void setLetters(char string1storage[], int intarray[])//runs through all characters in string1storage and updates the letter count in intarray
    {
    	int index;
    	char ch;
    
    	index = 0;
    	while(string1storage[index])
    	{//isalpha returns true if lower/uppercase letter
    		if(isalpha(string1storage[index]));//if true
    			string1storage[index] = 0;//"skip or delete whatever inside there", i'm stuck, i'm intending to delete or ignore non-alphabet chars in the array
    		index++;
    	}
    	
    	index = 0;
    	ch = string1storage[index];
    	while(string1storage[index])
    	{
    		//97-122--> decimals for ACSII are a-z lowercase
    		string1storage[index] = tolower(string1storage[index]);
    		index++;
    	}
    	
    	
    	printf("testing %s\n", string1storage); 
    
    	index = 0;
    	
    	index = (int) (ch - 'a');//do not replace the a,sees letters in string1storage and increments them by 1 in intarray[index]
    	
    	printf("testingtwo %d\n", index);
    }
    -thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    		if(isalpha(string1storage[index]));//if true
    			string1storage[index] = 0;//"skip or delete whatever inside there", i'm stuck, i'm intending to delete or ignore non-alphabet chars in the array
    		index++;
    Remove that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    how would i remove/ignore nonalphabet characters if i removed that?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Look carefully and match up the colors. The magenta 'that' refers to the magenta ';' at the end of your if line in the code block.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    o ok thanks, can i have more than one \0 in an array?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Sure, but when you treat it as a string, it will always stop at the first \0.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    o ok thanks, how would i print an array of integers? when i try to the output is a happy face

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Hard to say without knowing what piece of code is producing this error. My guess is you're using %c instead of %d. Read up on the printf documentation.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    if i use %d it produces garbage values

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Okay...what printf line is producing this problem? Can you give me some sample input so I can reproduce this?

    EDIT: And an up to date version of your code.
    Last edited by anduril462; 04-26-2011 at 11:58 AM.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    19
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define MAX 100
    #define LETTERS 26
    //void initialize(int intarray[]);//initializes strings probably
    //char *gets_s(char s[], int num);, terminates a string with \0
    void getString(char string1storage[], char string2storage[]);
    void setLetters(char string1storage[], int intarray[]);
    void checkLetters(char string2storage[], int intarray[]);
    void main(void)
    {//strings need2be initialized with anagram first?
    	
    	char string1storage[MAX], string2storage[MAX];//stores anagrams
    	int intarray[LETTERS] = {0};
    	//initialize(); //maybe call him after getstring?
    	getString(string1storage, string2storage);
    	setLetters(string1storage, intarray);
    	checkLetters(string2storage, intarray);
    }
    
    /*void initialize(intarray[])
    {
    	intarray[]={0};
    }*/
    
    void getString(char string1storage[], char string2storage[])
    {	
    	printf("Please enter a word or phrase");
    	gets_s(string1storage, MAX - 1);
    	printf("Please enter a second word or phrase");
    	gets_s(string2storage, MAX - 1);
    }
    
    void setLetters(char string1storage[], int intarray[])//runs through all characters in string1storage and updates the letter count in intarray
    {
    	int index, indexforintarray;
    	//char ch;
    
    	/*index = 0;
    	while(string1storage[index])
    	{//isalpha returns true if lower/uppercase letter
    		if(isalpha(string1storage[index]))//if true
    			string1storage[index] = '\0';//"skip or delete whatever inside there", i'm stuck, i'm intending to delete or ignore non-alphabet chars in the array
    	//use null^?
    			index++;
    	}
    	printf("%s\n", string1storage);*/
    	index = 0;
    	//printf("%d\n", isalpha(string1storage[0]));
    	while(string1storage[index])
    	{
    		string1storage[index] = tolower(string1storage[index]);
    		index++;
    	}
    	
    	indexforintarray = 0;
    	index = 0;
    	while(string1storage[index])//for(index = 0; index != '\0'; index++)//use on string1storage
    	{
    		indexforintarray = (int) (string1storage[index] - 'a');
    		//intarray[indexforintarray] = string1storage[index];//wtf is this
    		if(indexforintarray >= 0 && indexforintarray <= 25)//probably ignores .!123*+ etc., makes isalpha unneeded
    			intarray[indexforintarray]++;
    		index++;
    	}
    	/*printf("testing %s\n", string1storage); 
    	printf("testing %d\n", intarray); 
    	printf("testing %s\n", intarray); */
    }
    
    void checkLetters(char string2storage[], int intarray[])
    {
    	int indexforintarray, lettermatch, index;
    
    	index = 0;
    	//printf("%d\n", isalpha(string1storage[0]));
    	while(string2storage[index])
    	{
    		string2storage[index] = tolower(string2storage[index]);
    		index++;
    	}
    	
    	indexforintarray = 0;
    	index = 0;
    	while(string2storage[index])//for(index = 0; index != '\0'; index++)//use on string1storage
    	{
    		indexforintarray = (int) (string2storage[index] - 'a');
    		//intarray[indexforintarray] = string1storage[index];//wtf is this
    		if(indexforintarray >= 0 && indexforintarray <= 25)//probably ignores .!123*+ etc., makes isalpha unneeded
    			intarray[indexforintarray]--;
    		index++;
    	}
    
    	lettermatch = 0;
    	for(indexforintarray = 0; indexforintarray < 26; indexforintarray++)
    	{
    		if(intarray[indexforintarray] > 0)//avoids adding negative numbers
    			lettermatch+=intarray[indexforintarray];
    	}
    
    	if(lettermatch == 0)
    		printf("Your inputs are anagrams");
    	else
    		printf("Your inputs are not anagrams");
    
    }
    i dont understand why my setletters output isn't showing in the program
    postedit:edited the code twice sorry, works perfectly now, but i want to integrate the initialize function in my program now for usage.
    Last edited by nospammax; 04-26-2011 at 12:15 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    It's int main(void) and return an integer at the end, usually 0.

    Code:
    	if(lettermatch == 0)
    You need a second = here. One = is for assignment, two == is for comparison.

    i dont understand why my setletters output isn't showing in the program
    Perhaps because you commented out all the print statements? Your two loops in there can be merged into one that ignores all non-alpha chars:
    Code:
    index = 0;
    for each character in string1storage
        if isalpha is true
            convert it to lower
            subtract 'a' from it
            increment the count of that index in intarray
    checkLetters will do something very similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. It's your turn (job anagrams)
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-18-2007, 12:18 PM
  2. anagrams problems with n factorial
    By treenef in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2005, 12:52 AM
  3. anagrams
    By InvariantLoop in forum C Programming
    Replies: 12
    Last Post: 03-10-2005, 04:22 PM
  4. Anagrams
    By misswaleleia in forum C Programming
    Replies: 2
    Last Post: 05-31-2003, 06:37 PM
  5. Anagrams
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-08-2002, 03:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21