working with char

This is a discussion on working with char within the C++ Programming forums, part of the General Programming Boards category; k i will tell you the part i can't get and will not explain the whole thing. Anyways i need ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    working with char

    k i will tell you the part i can't get and will not explain the whole thing.
    Anyways i need someway to get the c++ program to see "what" same as "hwat" so i want to rearange it i got the enlish but now i just need to compare them.
    I though that if i add w+h+a+t it would be the same as h+w+a+t but it is not it game me two diffirent number.
    "I thought that each letter has same value."
    Thanks let me know if it is to confusing.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what makes you think the sum of the two strings are not the same?
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string s1 = "hwat";
    	string s2 = "what";
    	int i1 = 0, i2 = 0;
    	int i;
    	for(i = 0; i < s1.size(); i++)
    		i1 += s1[i];
    	for(i = 0; i < s2.size(); i++)
    		i2 += s2[i];
    
    	cout << "i1: " << i1 << " i2: " << i2 << endl;
    
    	return 0;
    }

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    I'm really not sure what you're trying to say. If you're asking how to compare a word to user input and consider them the same word if there is only a minor misspelling, then I have to ask a few questions before I can really give you an idea of what to do. First, what do you mean "get the program to see?" Are you talking about user input that the program will have to compare to a literal constant in the program? If this is the case, then I'll ask how long is this constant. Because if it's only four letters like what, then I would just use the "or" operator to compare to all the misspellings, if it's a long string, then it's more difficult and I'll get to that in a reply.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    no i am doing this challange that is scrambeled words and u have to get to normal and i got everything set up. I got it to see if it the same number of letters then see if it is the same letters in it. if so that means there is a chance i found unscrambeled word.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you could sort all the letters in ascending (or descending) order, then compare the two strings. That does not mean the two original strings are the same, it only means that the two strings contain the same letters.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Why don't you just check to see if it's the exact same word?

    For instance you supply : gmigrpmoarn
    They have a chance to answer.
    If what they input is "Programming" they win. If it isn't, they guess again. I don't see why you need to first check if it contains the same letters.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    accient hit it right in the spot now i need a good english txt file taht has no spaces cause i am tired of same word like 100 times and why does it make "hwat" and it says "salt" has same number.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    So now you've rearranged the user's string to an incorrect order and can check to see if all the letters are the same? How do you know check to see if they got the word correct because their input is now not what their input was.

    As AD said...

    Quote Originally Posted by Ancient Dragon
    That does not mean the two original strings are the same, it only means that the two strings contain the same letters.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    and why does it make "hwat" and it says "salt" has same number.
    because the sum of their individual letters are the same Here is the output when s1 = "hwat" and s2 = "salt". You should experiment yourself to see why they are the same.
    Code:
    i1: 104 119 97 116
    i2: 115 97 108 116
    i1: 436 i2: 436
    Press any key to continue

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    Thanks guys i am not spell chekcing i am rearanging a word like a puzzle and how can i over come that salt and hwat problem can i assing diffirent number to each of the letters will that fix it. thanks

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    while i sit and cry here.....
    and saying ""o plz"

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    assigning different numbers will not fix it, every letter already has a unique number. You might investigate hash algorithms -- boost libraries has one (or more), but I've never used them. google for "c++ has algorithms" and you will find more information. I don't know if they will help you with your problem or not. But just simple addition of the individual characters is not sufficient to obtain a unique numeric value for any given word.
    Last edited by Ancient Dragon; 11-06-2005 at 08:53 PM.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    hmm anyother ideas?
    lol i am out.
    the only thing i can think of is getting a string and see if there is other same letter and doing it with each one. by useing the value.

  14. #14
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you'll have to rearrange the letters in every possible order until the strings match(if they do). The algorithm isn't that hard to figure out and you can use _stricmp() to test each possibility.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    k any good tutorials on rearanging the strings

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM

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