oh damn i put getline...ooops
ok...how do i compare them is what i wanted to ask in that bool function?
Main Topics
Browse All TopicsHi...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;
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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
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.
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.u
I hope this helps. :-)
I found this to but i could not compile the sample.
http://www-cse.ucsd.edu/~c
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
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.
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.
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.
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/f
** Subjects/cc1002/1997/notes
**************************
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!
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;
}
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;
}
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.
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!
#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.
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
??
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!
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++;
}
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(..)
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.
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.
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,arra
//You can test to uncomment the line below and if you do don't forget
//to include windows.h
cout<<"array1="<<array1<<"
//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);
}
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)
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!
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
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,L
char mystring[256];
char mymessage[256];
int main()
{
//cout << "Welcome to Anagram Detector!\n\n>> ";
strcpy(mymessage,"Welcome to Anagram Detector!");
DialogBox(0,MAKEINTRESOURC
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,MAKEINTRESOURC
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,myme
GetWindowText(GetDlgItem(h
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_AUTOHS
END
__________________________
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.
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
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
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_AUTOHS
END
3. Make sure this file is added to you project.
4. Compile and run.
:-)
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!
Business Accounts
Answer for Membership
by: GleasonGuyPosted on 1999-12-09 at 09:37:50ID: 2269299
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.