Anagram finding function

Hi...I know this is for school...this is what I have so far.  the idea of the program is to use two strings.  string one the user inputs a word and the next string an anagram of that word, ie., string 1: dear, string 2: dare

and naturally output if is passes or not.

this is a beginner's course below is what i have as of now.

thanks!

(i need help in figuring out how do i figure out if it is an anagram??!!)


#include <iostream.h>
#include <ctype.h>

bool anagram(char [], char []);

int main() {
      cout << "Welcome to Anagram Detector!\n\n>> ";
      char ch, array1[256], array2[256];
      int i = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array1[i] = ch;
            i++;
      } while (ch != '\n');
    array1[i] = '\0';

      cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
      int j = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array2[j] = ch;
            j++;
      } while (ch != '\n');
    array2[j] = '\0';

// make an if to call function??

      return 0;
}

bool anagram(char test1[], char test2) {

// i know this does absolutly nothing
return true;
}
weinrjAsked:
Who is Participating?
 
andlaConnect With a Mentor Commented:
This MessageBox is implemented as a standard control in you visual studio. If you want it to display an editbox in an dialogbox it will be more work. And since you work in the console enviroment it will be more problem. I can make simple working app for you but it will take you several days to get into. You vill have to learn how windows handle messages. There are two impotant parts a message sender and a message retriever. Remember don't stay to long with old C/C++. If you want to do the cool stuff you chould study directx sdk, windows programming and MFC.

About the print yes and no i only say it would not right to help you. I think your teacher ment that you should study. :-)
hint: Just read documentation about 'if'

Good luck!
0
 
GleasonGuyCommented:
What exactly are you having problems with? Getting the strings or comparing them?

If it is comparing them, I think I would strip out the whitespace, sort the characters in each string and then compare the results. If they are equal, then you have an anagram.
0
 
weinrjAuthor Commented:
oh damn i put getline...ooops

ok...how do i compare them is what i wanted to ask in that bool function?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
weinrjAuthor Commented:
wait
i did that for the \n

how do i get rid of whitespace?
0
 
GleasonGuyCommented:
There are often many ways to do what you want. I'm not sure what is required by your class.

To detect whitespace, you can use isspace(). You can use isspace to determine wheter to copy a character from your original string or not.

To compare two character array you can use strcmp().
0
 
weinrjAuthor Commented:
ok but when comparing will dear and dare be same? or dare and dare?

requirements are minimal, just to use tolower, and detect if anagram in function.  ( i like bool so i am doing this )

this program can be complex as i want it to be...

i will attempt to do this

pseudo code

compare string1 and string2 using function

function

if true then return true
if false,then return false

print out accordingly

0
 
GleasonGuyCommented:
Your general pseudo code looks code.
Now you should post some for your compare function and I'll let you know what your missing.
0
 
weinrjAuthor Commented:
this is what i cant think of how i am going to do this

i think i need a counter
and thats about it

0
 
GleasonGuyCommented:
I think I would try the following...

- Compare string1 and string2
- return 1 if they are identical
- copy string1 to temp1 discarding whitespace and converting to lowercase
- copy string2 to temp2 discarding whitespace and converting to lowercase
- sort the characters in temp1
- sort the characters in temp2
- if length of temp1 != length temp2, return 2
- compare temp1 and temp2
- return 0 if they are identical
- return 2 if they are not.

dear, dare would return 0
dare, dare would return 1
dear, deer would return 2

0
 
weinrjAuthor Commented:
ah have a true, and false and equals...but i would like to use a bool functions (to show off)
0
 
GleasonGuyCommented:
Sure you can return a bool. It's your function you can do wahtever you want.
I think it would be more robust to return multiple values. After you've looked at the prototype for a lot of compare functions, like you are writing, 0 is usually success and non-zero is an error code.

If you look at the strcmp() function it returns -1 for a<b, +1 for a>b, and 0 for a==b. This is the kind of thing I was suggesting.

As for using boolean expressions you can say !function() means success.

But you shouldn't worry so myuch about the return value. Try coding up the function and getting it to work. Use the debugger or print commands to check your algorithm.

Good luck.
0
 
weinrjAuthor Commented:
yes i agree it will be more robust...but it just wants a yes or no, so bool seemed logical.  if it does the minimum u get the A, if u go all the way, he doesnt do anything extra special.  

I got work later tonight so I will work on this tomorrow.  I believe the due date is 20 Dec.
0
 
andlaCommented:
This is a samplecode that i found on an anagram. It must have like all the angrams a dictionary to find words. I you compile and run this program you can use:
anagram.exe -dDictionary.txt -oOutputfile.txt Mystring

if you have a file named Dictionary.txt
and wants the output to file Outputfile.txt and test the work 'Mystring' without "" or ''

http://www.brevard.cc.fl.us/wbcc/staff/harry/cprogs/anagram11.c

I hope this helps. :-)
0
 
andlaCommented:
I found this to but i could not compile the sample.

http://www-cse.ucsd.edu/~calder/classes/spring99/cse231/course_work.html
0
 
weinrjAuthor Commented:
way too complex....
0
 
andlaCommented:
Everthing looks complex at the first sight. Please explain what i can do for you? What kind of tools (functions, classes) do you want?
0
 
weinrjAuthor Commented:
just one function (not including main)

everything is done in main
function just determines if it is an anagram or not.

by the true or false return, it will display output accordingly
0
 
andlaCommented:
So if i create a function that for example a user put in value for example millenum and it found these:
Immune i'll
Immune ill
I'll menu i'm
I'll 'em i'm unMenu mill i
Menu i'm ill
Mien mull i
Mill 'em un i
Mill me un i
Mine mull i
Mule i'm nil
Mull 'em in i
Mull in me i
Null 'em i'm i
Null i'm me i
I'll i'm me un

'em i'm ill un
Ell mum in i
I'm ill me un
Lie mum nil

it will return TRUE witch is the string of all hits.
If no hits where found it will return FALSE
0
 
GleasonGuyCommented:
andla, weinrj is not looking for a function to CREATE an anagram. He is looking for a function to COMPARE two strings and determine if they are anagrams of each other.

This is a homework/lab problem, so please do not post the function for weinrj. I have given him pseudo code for the function already (which in hindsight was probably too much).

weinrj, I think that you should be able to get the function coded without too much difficulty. Once you have it coded, post any specific questions and we can help you out.
0
 
andlaCommented:
GleasonGuy->"
This is a homework/lab problem, so please do not post the function for weinrj. "

You must be the school-teacher ? "joke" :-)

I suppose i can help how to build the function.
0
 
GleasonGuyCommented:
No, I'm not a teacher. It is grounds to be kicked off Experts Exchage to post homework solutions for people. It is OK to answer specific questions or to help with concepts. It is NOT OK to do the work for the person.

The point of homework and labs is to turn the student into an Expert in their own right.

As I said, I've already provided pseudo code. Let weinrj give it a shot.
0
 
andlaCommented:
GG>> I read the agreement and nowhere i read about that you said. I agree with you that not to help cheeting in the homework. I don't agree with you that people that are in trouble can't get any help because that would be to do all the work for that person. I read that i can't using usernames without permission so for that i'm sorry. I will use initials instead.
0
 
TallBoyCommented:
Weinrj,

As someone pointed out, the easiest way for you to do it is to sort the two strings, then compare them.  If they are identical, then they are anagrams.

For sorting, I usually use the QuickSort algorithm.  My example here was compiled under MSVC++ 6.0.

------------------

void CSDlg::QuickSort( CString strList[], int low, int high )
{
      /**************************************************
      ** strList = Array of Cstrings                   **
      ** l       = Low boundary of array (usually 0)   **
      ** r       = High boundary of array              **
      **                                                                       **
      ** All Strings are temporarily converted to      **
      ** lower case for the sort.                      **
      **                                                                       **
      ** Adapted from code at:                                   **
      **  http://www.cs.jcu.edu.au/ftp/web/teaching/   **
      **  Subjects/cc1002/1997/notes/lect17/qsort.html **
      **************************************************/

      CString pivot;
      int left, right;
      
      left = low; right = high;
      pivot = _strlwr( _strdup( strList[ (left + right) / 2 ] ) );
      while (right >= left)
      {
            // find the next elements on the wrong
            // sides of the pivot and swap them
            while ( _strlwr( _strdup( strList[right] ) ) > pivot) right--;
            while ( _strlwr( _strdup( strList[left] ) )  < pivot) left++;
            if( left <= right )
            {
                  swap( strList, left, right  );
                  left++;   right--;
            }
      }

      // recursively sort the lower and upper
      // parts of the array
      if (low < right) QuickSort(strList, low, right);
      if (left < high) QuickSort(strList, left, high);

}

----------------

Note that I use CStrings, but the algorithm will still work with chars.

Next, you do a compare, so I'd use

if( strcmp(str1, str2)==0 )
  cout << "Anagram!\n";
else
  cout << "Not an anagram.\n";

If you're still using CStrings, then you can just use

if( str1 == str2 )
 ...

and so on, and you're done.

-:-

Now, if you want to make the thing check for complex anagrams, such as those that are multiple words, then you're going to have to strip out the spaces.

To do this, there are several different ways.

1) Using CStrings (this is one reason why I love 'em!), you can use TrimLeft(str) or TrimRight(str).
2) For chars, you're going to have to do a bit more work:

/* assume str is the string to strip */
char* temp;
int i;
for( i = 0; i < strlen( str ); i++ )
{
  /* Append to temp if not a space */
  if( str[i] != ' ' )
    strcat( temp, str[i] );
}

/* make temp end with a null, for safety */
strcat( temp, '\0' );

....

Now your temp variable contains what str did without all the spaces.  You compare it as above.

Good luck!
0
 
weinrjAuthor Commented:
i will keep this locked as u gave me a lot of complex info.

i will see how i handle this over the weekend.
0
 
andlaCommented:
Just ask and we will answer. :-)
0
 
weinrjAuthor Commented:
i agree that a sort would be best but you gave me code.  i dont understand it!

so what i want to do is sort string1 and string2

then i will do a string compare

i get that part -- how do i sort the text??  quick sort may be too complex for me.  what about bubble or insertion?  thanx
0
 
GleasonGuyCommented:
A bubble sort on each string would do just fine for you.

You want it so that "dare" would become "rdea" (or "aedr" depending on the direction you sort).

0
 
ozoCommented:
#include <stdlib.h>
void qsort (void* base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));
0
 
weinrjAuthor Commented:
why wouldnt it be ader?  i am making the letter fully in alphabetical order right?  so dare become ader
and read become ader so when i do a compare they are equal thus an anagram.  right?
how do i sort chars in a bubble?  man i didnt do sorts in awhile, back to the text book!
0
 
weinrjAuthor Commented:
thus far is this
let me know before i go on

#include <iostream.h>
#include <ctype.h>
#include <string.h>

bool anagram(char [], char []);

int main() {
      cout << "Welcome to Anagram Detector!\n\n>> ";
      char ch, array1[256], array2[256];
      int i = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array1[i] = ch;
            i++;
      } while (ch != '\n');
    array1[i] = '\0';

      cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
      int j = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array2[j] = ch;
            j++;
      } while (ch != '\n');
    array2[j] = '\0';


      return 0;
}

bool anagram(char test1[], char test2[]) {

// Sort both algorithms
      // Compare algorithms

      int i, hold;

      for ( int pass = 0; pass < strlen(test1) - 1; pass++ )
            for ( i = 0; i < strlen(test1) - 1; i++ )
                  if (test1[i] > test1[i + 1]){
                        hold = test1[i];
                        test1[i] = test1[i+1];
                        test1[i+1]=hold;
                  }

            cout << "Gore: ";
            for ( i = 0; i < strlen(test1); i++ )
                  cout << setw(3) << test1[i];



return true;
}
   





0
 
weinrjAuthor Commented:
this code is better...i havent compiled it yet, but this is what i got so far...i commented a line towards the bottom .  thnx

#include <iostream.h>
#include <ctype.h>
#include <string.h>

bool anagram(char [], char []);

int main() {
      cout << "Welcome to Anagram Detector!\n\n>> ";
      char ch, array1[256], array2[256];
      int i = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array1[i] = ch;
            i++;
      } while (ch != '\n');
    array1[i] = '\0';

      cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
      int j = 0;
      do {
            cin.get(ch);
            ch = char(tolower(ch));
            array2[j] = ch;
            j++;
      } while (ch != '\n');
    array2[j] = '\0';
   
      cout << "Is this an anagram: " << anagram(array1,array2);
      // if true print YES if false, print NO

      return 0;
}

bool anagram(char test1[], char test2[]) {

// Sort both algorithms
      // Compare algorithms

      int i, hold, j, held;

      for ( int pass = 0; pass < strlen(test1) - 1; pass++ )
            for ( i = 0; i < strlen(test1) - 1; i++ )
                  if (test1[i] > test1[i + 1]){
                        hold = test1[i];
                        test1[i] = test1[i+1];
                        test1[i+1]=hold;
                  }

    for ( int pass1 = 0; pass1 < strlen(test1) - 1; pass1++ )
            for ( j = 0; j < strlen(test2) - 1; i++ )
                  if (test2[i] > test2[j + 1]){
                        held = test2[j];
                        test2[j] = test2[j+1];
                        test1[j+1]=held;
                  }

    if ( test1 == test2 )
            return true;
      else
            return false;
            
}    





0
 
GleasonGuyCommented:
Sorry about that... it would be "ader"... I gues my internal alphabet routtine needs some work ;-)

Your bubble sort looks OK to me. It will get the job done although it is not optimized.

Are you going under the assumption that there will be no whitespace? For example, "dare" and "read", but not "a red".

If "a red" is OK then you have to strip out whitespace before you sort.

Also, I would put your nested for loops in their own function called SortString() or something. You will need to use it twice. Once for each string.
0
 
weinrjAuthor Commented:
in 2nd for loop, i forgot to made tes1[j+1], its test2...but i know this is wrong since the loop algorithm is for int not chars, so i need help with that
0
 
GleasonGuyCommented:
It looks like it should work. Compile it and run some test cases.

For the return of anagram, you can simplify your code by putting...

return( test1 == test2);
0
 
GleasonGuyCommented:
That's why cut and paste is bad and functions are good.

You also are incrementing i in the second loop instead of j.
0
 
weinrjAuthor Commented:
i got four warnings '<' signed/unsigned mismatch

it ran but after the 2nd was inputted it froze or stopped
0
 
weinrjAuthor Commented:
and the output needs working on for if it is one or not
0
 
GleasonGuyCommented:
Can you tell where it is stopping? If not you can use the debugger to trace through your code. Or you can put cout statements in different places in your code to see if it reaches them.

FYI,
ch = char(tolower(ch));
array1[i] = ch;
i++;

can be combined to
array1[i++] = char(tolower(ch));
0
 
andlaCommented:
Remember that cin.get(n) leaves a new line (\n) in the buffer. Next time when the loop gets to cin.get it will not wait for you because it thinks you pressed the return key.

I tok some time to write a new code piece of the input snippet.

while(1)
{
      ch=cin.get();
      if(ch=='\n')
      break;
      cin.get();
      ch = char(tolower(ch));
      array2[j] = ch;
      j++;
}

Good luck!
0
 
weinrjAuthor Commented:
#include <iostream.h>
#include <ctype.h>
#include <string.h>

bool anagram(char [], char []);

int main() {
      cout << "Welcome to Anagram Detector!\n\n>> ";
      char ch, array1[256], array2[256];
      int i = 0;
      do {
            cin.get(ch);
            array1[i++] = char(tolower(ch));

      } while (ch != '\n');
    array1[i] = '\0';

      cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
      int j = 0;
      while(1)
      {
            ch=cin.get();
            if(ch=='\n')
                  break;
            cin.get();
          ch = char(tolower(ch));
            array2[j] = ch;
            j++;
}



    array2[j] = '\0';
      cout << "Is this an anagram: " << anagram(array1,array2);
      // if true print YES if false, print NO

      return 0;
}

bool anagram(char test1[], char test2[]) {



      int i, hold, j, held;

      for ( int pass = 0; pass < strlen(test1) - 1; pass++ )
            for ( i = 0; i < strlen(test1) - 1; i++ )
                  if (test1[i] > test1[i + 1]){
                        hold = test1[i];
                        test1[i] = test1[i+1];
                        test1[i+1]=hold;
                  }

    for ( int pass1 = 0; pass1 < strlen(test2) - 1; pass1++ )
            for ( j = 0; j < strlen(test2) - 1; i++ )
                  if (test2[j] > test2[j + 1]){
                        held = test2[j];
                        test2[j] = test2[j+1];
                        test2[j+1]=held;
                  }

    return (test1 == test2);
            
}    


after i input the 2nd string it supposed to check the string.

is there an error in the function?  is there an error in calling the function?

is there an error because I am not printing anything???

there should be...there is an output before the function is called...i think something is happening before the function is being called...but would this function still would with chars?? i dont see why not.

but i still get that signed / unsigned mismatch - its w/ the nested for pass for i for pass1 for j loops

man this is sure puzzling - i thought i had the problem solved and now it compiles but it doesnt do things right.
0
 
andlaCommented:
First you put 'unsigned' everywhere you are sure no negative value can exist. This will also give you ability to put a higher number. Twice as big.

unsigned int i, hold, j, held;
for(unsigned int pass....
for(unsigned int pass....

I will check more...
0
 
weinrjAuthor Commented:
i doubt the warnings are causing the problem but they could be....

but if u look here: cout << "Is this an anagram: " << anagram(array1,array2);
// if true print YES if false, print NO



the cout is never printed, meaning the function is never called...somewhere else long the line has a problem....then when i get that printed i will see what is wrong w/ the function if anything....then i need to figure out in the cout if it passed or not and how to say that...

like if (true) cout << yes
if (false) cout << no

??
0
 
andlaCommented:
I just helped you get rid of the warnings. And along the way i found you program holds.

It's so simple yet hard to find so tell you about the story of 'j'

Once upon a time there where a 'j' that couldn't grow up. He was still young and he relly wanted to to tell the
for ( j = 0; j < strlen(test2) - 1; i++ )

loop that he wanted to grow up so he could leave the for loop. But 'i' told sorry you are to young so i have to look after you. This is the end of the sad story.

If you replace 'i' with 'j' the story ends with a happy ending.

Good luck!
0
 
andlaCommented:
The program works better if you remove the cin.get();

while(1)
{
cin.get(ch);
                  if(ch=='\n')
                  break;
                  //cin.get();
array1[i++] = char(tolower(ch));

}
    array1[i] = '\0';

cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
int j = 0;
while(1)
{
                  ch=cin.get();
                  if(ch=='\n')
                  break;
                  //cin.get();
                ch = char(tolower(ch));
                  array2[j] = ch;
                  j++;
}

0
 
andlaCommented:
The '\n' will alway be the last character in the buffer so it will never go in your strings.

Remember that the buffer i'm talking about is not your strings. It's kept somwhere in the data that iostream.h loaded. These data is called object.
There is an istream object for cin and a ostream object for cout. If you take a look at the tutorial for iostream you will find out that you can fix the loops that create your strings. For example take a look at cin.getline(..)


0
 
weinrjAuthor Commented:
ok...i put unsigned...the warnings are now gone.

i stupidly didnt realize the j++ thing...ooops....but that is in the function.  my program has never called the function yet since my cout isnt couting (is that even a word?)

Hmm, it is something w/ the string input.  I was taught to use cin.get() with the '\n'....My teacher likes while loops but i told him it is redundent since he has two couts and cins while i have one since i use do..while.

he agreed.  so i have to wonder what to do with the string input.
0
 
andlaCommented:
I mostly use while loops but sometimes its easier to use do while.

What you see is a 1 or 0 .
I saw that output i think a zero. So i don't se any errors. But another question is if the function wants what you want. Or the function do what you wants but later you see this is no what you want any more. It's better to cut a big function into smaller that way you can reuse your code instead of writing the whole big function from scratch.

0
 
andlaCommented:
I think the teacher thinks it's better to not do it his way as long it works. BTW does he know about experts-exchange ? You found a goldmine. :-)
0
 
weinrjAuthor Commented:
He isnt that internet savvy as most of the students are...He does put assignments on the web though.  When I run this, after I input the 2nd string nothing happens.  it just sits there cursor flashing (i need to do a control c to stop it)

0
 
andlaCommented:
It works for me. This is what i got so far.


//#include <windows.h>
#include <iostream.h>
#include <conio.h>//using namespace std;
#include <ctype.h>
#include <string.h>

bool anagram(char [], char []);

int main()
{
cout << "Welcome to Anagram Detector!\n\n>> ";
char ch, array1[256], array2[256];
int i = 0;
while(1)
{
cin.get(ch);
                  if(ch=='\n')
                  break;
                  //cin.get();
array1[i++] = char(tolower(ch));

}
    array1[i] = '\0';

cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
int j = 0;
while(1)
{
                  ch=cin.get();
                  if(ch=='\n')
                  break;
                  //cin.get();
                ch = char(tolower(ch));
                  array2[j] = ch;
                  j++;
}



                  array2[j] = '\0';
                  //MessageBox(0,array1,array2,0);//i'm not sure if this works with Borland.
                  //You can test to uncomment the line below and if you do don't forget
                  //to include windows.h

                  cout<<"array1="<<array1<<"      array2="<<array2<<endl;
                  //endl makes a new line.
                  cout << "Is this an anagram: " << anagram(array1,array2);
                  // if true print YES if false, print NO

                  return 0;
}

bool anagram(char test1[], char test2[]) {



unsigned int i, hold, j, held;

for ( unsigned int pass = 0; pass < strlen(test1) - 1; pass++ )
      for ( i = 0; i < strlen(test1) - 1; i++ )
            if (test1[i] > test1[i + 1])
            {
                  hold = test1[i];
                  test1[i] = test1[i+1];
                  test1[i+1]=hold;
            }

for ( unsigned int pass1 = 0; pass1 < strlen(test2) - 1; pass1++ )
      for ( j = 0; j < strlen(test2) - 1; j++ )
            if (test2[j] > test2[j + 1])
            {
                  held = test2[j];
                  test2[j] = test2[j+1];
                  test2[j+1]=held;
            }

    return (test1 == test2);

}
0
 
andlaCommented:
Remember to use copy and paste and dont write this code by hand. If you do you will not have any typo in your code.
0
 
weinrjAuthor Commented:
ok

i use ms visual c++ 6.0
0
 
andlaCommented:
Same as i. :-)

Compile and run yeeeehaaa!!
0
 
weinrjAuthor Commented:
ok...this compiles here too.

what does the messagebox thing do?
for the class the box is not needed, but for my own self learning, how can i make a box pop up, ask for input w/ a box and output results w/ a box.

andla...thnx
0
 
weinrjAuthor Commented:
the next step for me is priting yes or no, not 0 1....heeh this is quite fun now, no wonder why i took this course!
0
 
andlaCommented:
I hope it works out. Just tell me if you got any serious problem.
:-)
0
 
weinrjAuthor Commented:
andla---

can u tell me how to make the messagebox done throughout the enire project?

such as

Enter String: [         ]
in a box

then enter anagram of string: [      ]

function runs

string 1 and string 2 are anagrams
of each other or not in another box...
i think that will be cool just for me to learn....(not for the school part though)

0
 
weinrjAuthor Commented:
since i use cout << anagram()

how do i say

if anagram returns true print yes
if anagram returns false print no

0
 
weinrjAuthor Commented:
ok---thanks!!!!!

I will LEARN this....

and i would like to learn the cool stuff while I am learning the console stuff.

If posible, please do that, dont matter how long....The windows version w/ that input out put diaglogs, etc....and send the source....i never done visual programming before just console, the window apps part is later on,.

please send to jwnrb@cybernex.net
0
 
weinrjAuthor Commented:
the anagram returns 0 no matter what

i cant do an if because the output of the number is out already
0
 
andlaCommented:
Here you go.

_______________________________________
//project.cpp


#include <windows.h>
#include <iostream.h>
#include <conio.h>//using namespace std;
#include <ctype.h>
#include <string.h>

bool anagram(char [], char []);
BOOL WINAPI DlgProc(HWND,UINT,WPARAM,LPARAM);
char mystring[256];
char mymessage[256];
int main()
{


//cout << "Welcome to Anagram Detector!\n\n>> ";
strcpy(mymessage,"Welcome to Anagram Detector!");
DialogBox(0,MAKEINTRESOURCE( 10000 ),0,(DLGPROC)DlgProc);
char  array1[256], array2[256];
strcpy(array1,mystring);



//cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
strcpy(mymessage,"Please enter an anagram of the following:");
strcat(mymessage,array1);
DialogBox(0,MAKEINTRESOURCE( 10000 ),0,(DLGPROC)DlgProc);
strcpy(array2,mystring);


cout << "Is this an anagram: " << anagram(array1,array2);
// if true print YES if false, print NO
//*/
return 0;
}


BOOL WINAPI DlgProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
      


      switch(uMsg)
      {      
            


               case WM_COMMAND:      
                  
                    
                        SetWindowText(hwndDlg,mymessage);
      

                  GetWindowText(GetDlgItem(hwndDlg,101),mystring,255);
                   if( wParam == IDCANCEL )
                  {
                  
                        EndDialog( hwndDlg, TRUE );
                        //PostQuitMessage(wParam);
                  
                   return( TRUE );  
                        //break;
                  }  
                  
                  
      }
return( FALSE );
}









bool anagram(char test1[], char test2[]) {



unsigned int i, hold, j, held;

for ( unsigned int pass = 0; pass < strlen(test1) - 1; pass++ )
for ( i = 0; i < strlen(test1) - 1; i++ )
if (test1[i] > test1[i + 1])
{
hold = test1[i];
test1[i] = test1[i+1];
test1[i+1]=hold;
}

for ( unsigned int pass1 = 0; pass1 < strlen(test2) - 1; pass1++ )
for ( j = 0; j < strlen(test2) - 1; j++ )
if (test2[j] > test2[j + 1])
{
held = test2[j];
test2[j] = test2[j+1];
test2[j+1]=held;
}

    return (test1 == test2);

}    
______________________________________
//resource.rc
  #include <windows.h>

10000 DIALOG  DISCARDABLE  0, 0, 186, 95
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My DialogBox"
FONT 8, "MS Sans Serif"
BEGIN
 EDITTEXT        101,11,34,160,15,ES_AUTOHSCROLL  
END
_______________________________________



0
 
andlaCommented:
Did you know that you can put functions in an if() ?

Try again. :-)
0
 
weinrjAuthor Commented:
in an if?? cool...thnx
0
 
weinrjAuthor Commented:
how do i insert resource script....also in my original lab i always get false no matter what....crazy
0
 
weinrjAuthor Commented:

  if (anagram(array1,array2) == true)
        cout << "YES!  Thats an anagram.";
  else
        cout << "NO!   Thats not an anagram.";
 

but it always says no....

and whats funnier is i did a debug

 cout << endl << "test1: " << test1 << endl << "test2: " << test2 << endl;

and it shows it works....
0
 
andlaCommented:
I believe an anagram is harder to make than that. When i studied the code listings they ususally used an extra file containing information that the anagram program could use. I don't really know what you have in plan but this code (test2[j] > test2[j + 1]) will check the ascii value and sort the characters in the string according to that value. I don't know much about anagram but if you give me a clear definition i can try to help you.
0
 
weinrjAuthor Commented:
how do i do the resource script??

from the other thing


"
....whether one is an anagram of the other -- that is, whether one character string is a permutation of the characters in the other string. For example, "dear" is an anagram of "read, as is "Dare". Note that we do not distinguish between uppercase and lowercase letters. " from the assignmetn
0
 
weinrjAuthor Commented:
just so u know----i got the program to work

/*
   Jonathan Weinraub
   anagram finder 2000

*/

#include <iostream.h> // input / output stream
#include <ctype.h>    // get
#include <string.h>   // string functions


bool anagram(char [], char []); // declaration statement

int main()
{
   cout << "Welcome to Anagram Detector!\n\n>> ";
   char ch, array1[256], array2[256]; // initialize strings to 256 charactors
   int i = 0; // initialize counter to zero
   while(true)   // starts loop  - also prevents '\n' from being stored
   {                     // in the array buffer
     cin.get(ch);
     if(ch=='\n')
        break;

   array1[i++] = char(tolower(ch));
}
   array1[i] = '\0'; // appends null charactor  
   cout << "\nPlease enter an anagram of the following: " << array1 << "\n>> ";
   int j = 0;
   while(true)
   {
     ch=cin.get();
     if(ch=='\n')
        break;
     ch = char(tolower(ch));
     array2[j] = ch;
     j++;
   }

  array2[j] = '\0';
 
 
   
 
  if (anagram(array1,array2) == true)
        cout << "YES!  Thats an anagram.";
  else
        cout << "NO!   Thats not an anagram.";
 
 

  cout << endl << endl;
  return 0;
}

bool anagram(char test1[], char test2[]) {

  /* This function will determine if something is an anagram or not
  by sorting the string using a bubble sort then comparing the two
  strings */
      
  unsigned int i, hold, j, held;

  for ( unsigned int pass = 0; pass < strlen(test1) - 1; pass++ )
    for ( i = 0; i < strlen(test1) - 1; i++ )
      if (test1[i] > test1[i + 1])
        {
        hold = test1[i];
        test1[i] = test1[i+1];           // sorts string1 using bubble
        test1[i+1]=hold;
        }

  for ( unsigned int pass1 = 0; pass1 < strlen(test2) - 1; pass1++ )
    for ( j = 0; j < strlen(test2) - 1; j++ )
      if (test2[j] > test2[j + 1])
        {
        held = test2[j];
        test2[j] = test2[j+1]; // bubble
        test2[j+1]=held;
        }
        
   if (strcmp(test1, test2) == 0)
         return true;
   else
         return false;

}



now--i would like to learn how the other program works once i know how to do a resource script
0
 
andlaCommented:
Ok the resource script is very simple. One of the first thing you learn in windows programmming is resource script.

1. Create a new file named resource.rc
2. In this file paste the code i gave you.
#include <windows.h>

10000 DIALOG  DISCARDABLE  0, 0, 186, 95
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My DialogBox"
FONT 8, "MS Sans Serif"
BEGIN
 EDITTEXT        101,11,34,160,15,ES_AUTOHSCROLL  
END

3. Make sure this file is added to you project.

4. Compile and run.

:-)
0
 
weinrjAuthor Commented:
aight thnx
0
 
andlaCommented:
>>"dear" is an anagram of "read, as is >>"Dare"

I think you must have a script file of these words. The computer could not possible know what is words and what is not without a script to follow.

Did your teacher give any instruction how to create the anagram?

0
 
peakerchooCommented:
Hi weirj

I too am trying to do a similar thing that you are doing and am finding it more difficult that you.  I am wondering what did u write for the other files...
ie. ctype.h

thanks for your help
0
 
weinrjAuthor Commented:
hmm, i did this long ago.  i am trying to find my original source code.  but i belive what i did was do  a bubble sort.
so i sorted both arrays.

the reason why i used ctype was so i can convert all letters to one case, (lowercase).

once both strings were then converted to lowercase, and then bubble sorted, i had two strings now.
i then did a string compare.  if they were exactly the same, then they were a anagram.

i did this freshman year in college.  i am now a senior so i am sure i would had done it a bit differently now.  

good luck!
0
 
peakerchooCommented:
thanks for your help wienrj. you have been godsend.

One other question.

How do i write a function in there so that all non alphabetic characters are removed... ie. punctuation, spaces, digits.

and How do I write a function so that at the end it prompts me to see if I would like to repeat?
0
All Courses

From novice to tech pro — start learning today.