943,938 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16174
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 26th, 2005
0

How to assign my own numeric values to a char array?

Expand Post »
I have the following code:

#include <iostream>
using namespace std;

int main () {

char alphabet[]="abcdefghijklmnopqrstuvwxyz";
char word;
cout <<"Please input a word: " <<endl;
cin >> word;

int res = -1;

for (int i = 0; alphabet[i] != '\0'; i++)
{
if (alphabet[i] == word)
{
res = i;
}
}
if (res != -1)
{
cout << word << " has the number " << res << " in the array \n";
}
}

//Thanx to dave sinkula and zyruz for their help so far


What i want to do is to input a word and get as output a number - the problem is that i dont know how to assign my own values (numbers) to the char array. For instance i dont want a to be 0, b to be 1 etc - i want a to be 17, b to be 23 and so on - what i want to do is to assign to each letter my own value.

Any suggestions?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jepapel is offline Offline
18 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

This is a somewhat slow solution because you end up with an expensive search, but it does what you want. The alphabet string holds all of your valid characters and the values array has the corresponding value at each index. Once you have the index for alphabet, you can get the value from values:
C++ Syntax (Toggle Plain Text)
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
  7. int values[] = {
  8. 25,24,23,22,21,20,19,18,
  9. 17,16,15,14,13,12,11,10,
  10. 9,8,7,6,5,4,3,2,1,0
  11. };
  12.  
  13. char word[10];
  14.  
  15. std::cout<<"Enter a word: ";
  16. std::cin>> std::setw ( 10 ) >> word;
  17.  
  18. for ( int i = 0; word[i] != '\0'; i++ ) {
  19. for ( int j = 0; alphabet[j] != '\0'; j++ ) {
  20. if ( word[i] == alphabet[j] )
  21. std::cout<< values[j] <<' ';
  22. }
  23. }
  24. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

Thanx Narue - That is exaclly what i wanted!
P.S: I think u are the most pretty woman programer i saw so far
Thanx again!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jepapel is offline Offline
18 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

Couldn't you have just used the ascii value and then subtracted to get its value in the alphabet?

Sorry my bad, i didn't read well enough. Your either writing a hash routine or weak encryption program aren't ya?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Sauce is offline Offline
55 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

>Couldn't you have just used the ascii value and then subtracted to get its
>value in the alphabet?
Even if that were a solution, it would still be a poor one. You would be making a non-portable assumption about the character set. This trick is only guaranteed to work with the digit characters, '0' through '9'.

>Your either writing a hash routine or weak encryption program aren't ya?
My bet is for encryption.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

If you're allowed to do other encryption schemes, just use the XOR operator with a character mask. Byte-level mixing is quick and dirty.

Pretty easy to crack, though.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

>Pretty easy to crack, though.
Actually, if done correctly, the XOR encryption can be used for a one-time pad, which is virtually impossible to crack.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

Well, I suppose it could. My imagination probably isn't implementing it correctly, though.

I'm just going with my limited experience in wireless communications... all the class had to do was a simple encryption scheme so the master could talk to the slave robot and transmit simple information packets. So, we didn't get very complex with it.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

Ok now what i want to do is for every word that will be entered, to be added with the next word that will be inputed. Example: if i enter the word 'b' the output will be '24' - but if i continue entering words for instance i enter the letter 'e' which results to '21' in our case - i want 24 and 21 to sum up and give me a total of 45. More than that how can i make in c++ to recognize the 'space' character?I want this because i want to input a whole phrase, like: "I am a new programmer" and i want c++ to recognize the space characters and add each letter that i entered with its next one...! I hope i made myself clear on what i want! thanx in advance again for ur intrest to help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jepapel is offline Offline
18 posts
since Jul 2005
Jul 26th, 2005
0

Re: How to assign my own numeric values to a char array?

>how can i make in c++ to recognize the 'space' character?
Just add a space to the alphabet and a corresponding value, same as all of the other characters you want to recognize.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: class array problem!
Next Thread in C++ Forum Timeline: C++ from the WEB?!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC