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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

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

 
0
  #1
Jul 26th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #2
Jul 26th, 2005
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:
  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. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

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

 
0
  #3
Jul 26th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 55
Reputation: Sauce is an unknown quantity at this point 
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

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

 
0
  #4
Jul 26th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #5
Jul 26th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

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

 
0
  #6
Jul 26th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #7
Jul 26th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

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

 
0
  #8
Jul 26th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 18
Reputation: jepapel is an unknown quantity at this point 
Solved Threads: 0
jepapel jepapel is offline Offline
Newbie Poster

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

 
0
  #9
Jul 26th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #10
Jul 26th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 10955 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC