anagram program help

This is a discussion on anagram program help within the C++ Programming forums, part of the General Programming Boards category; that one works todd, except that anagrams don't count things like whitespace and punctuation...

  1. #46
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    that one works todd, except that anagrams don't count things like whitespace and punctuation

  2. #47
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by m37h0d View Post
    that one works todd, except that anagrams don't count things like whitespace and punctuation
    anagrams are word plays - they don't ever count anything!

    Oh, you mean the program. Yes, well, unfortunately, it counts everything. Therefore, use what you want and ignore the rest.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #48
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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 &#37;s Anagrams\n", (compare( &c1, &c2 ) ? "are" : "are not" ) ) ; 
    
    	return 0 ; 
    
    
    
    
    }
    Last edited by pjr5043; 04-28-2008 at 03:09 PM.

  4. #49
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #50
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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

  6. #51
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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