that one works todd, except that anagrams don't count things like whitespace and punctuation
that one works todd, except that anagrams don't count things like whitespace and punctuation
edit: i got it to work using this code .. now does anyone know if i should just count the letter .. or can an anagram use numbers as well?
Code:#include <iostream> #include <cctype> #include <cstring> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; struct characters { int letters[256] ; } ; void map(const char * s, struct characters * c) { memset( c, 0, sizeof(struct characters) ) ; while(*s) { if (*s != ' ') { c->letters[(int) tolower(*s)]++ ; } s++ ; } } int compare( const struct characters * c1, const struct characters * c2 ) { return !memcmp( c1, c2, sizeof(struct characters)) ; } int main() { struct characters c1 , c2 ; char s1[500]; char s2[500]; char mystring1[500]; char mystring2[500]; cout << "enter first string" << endl; cin.getline(mystring1, 500, '\n'); int a=0; for (int i=0; mystring1[i] !='\0'; i++){ if (isalpha(mystring1[i])){ s1[a]=mystring1[i]; a++; } } s1[a]='\0'; cout << s1 << endl; cout << "enter second string" << endl; cin.getline(mystring2, 500, '\n'); int b=0; for (int i=0; mystring2[i] !='\0'; i++){ if (isalpha(mystring2[i])){ s2[b]=mystring2[i]; b++; } } s2[b]='\0'; cout << s2 << endl; map(s1, &c1) ; map(s2, &c2) ; printf("The strings %s Anagrams\n", (compare( &c1, &c2 ) ? "are" : "are not" ) ) ; return 0 ; }
Last edited by pjr5043; 04-28-2008 at 03:09 PM.
Well, since you need to be writing in C++, the only thing you should be taking out of my sample is the concept of how to do it.
Todd
Mainframe assembler programmer by trade. C coder when I can.
well i don't fully understand how your function works .. for instance .. i do not know what struct or memset are
if you were able to explain the function a little more i would understand it .. but like i said what i have now works
Code:mem.h, string.h Category Memory and String Manipulation Routines, Inline Routines Prototype void *memset(void *s, int c, size_t n); void *_wmemset(void *s, int c, size_t n); Description Sets n bytes of a block of memory to byte c. memset sets the first n bytes of the array s to the character c. Return Value memset returns s.
Code:Category Type specifiers Syntax struct [<struct type name>] { [<type> <variable-name[, variable-name, ...]>] ; . . . } [<structure variables>] ; Description Use a struct to group variables into a single record. <struct type name> An optional tag name that refers to the structure type. <structure variables> The data definitions, also optional. Though both <struct type name> and <structure variables> are optional, one of the two must appear. You define elements in the record by naming a <type>, followed by one or more <variable-name> (separated by commas). Separate different variable types by a semicolon. To access elements in a structure, use a record selector (.) or ->. To declare additional variables of the same type, use the keyword struct followed by the <struct type name>, followed by the variable names. In C++ the keyword struct can be omitted. Note: C++Builder allows the use of anonymous struct embedded within another structure.
learning to read manual-ese is by far the most valuable skill you will ever learn in programming.