954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

I have the following code:

#include
using namespace std;

int main () {

char alphabet[]="abcdefghijklmnopqrstuvwxyz";
char word;
cout <<"Please input a word: " <> 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?

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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:

#include <iomanip>
#include <iostream>

int main()
{
  char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
  int values[] = {
    25,24,23,22,21,20,19,18,
    17,16,15,14,13,12,11,10,
    9,8,7,6,5,4,3,2,1,0
  };

  char word[10];

  std::cout<<"Enter a word: ";
  std::cin>> std::setw ( 10 ) >> word;

  for ( int i = 0; word[i] != '\0'; i++ ) {
    for ( int j = 0; alphabet[j] != '\0'; j++ ) {
      if ( word[i] == alphabet[j] )
        std::cout<< values[j] <<' ';
    }
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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!

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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?

Sauce
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

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

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

ok beauty! thanx

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

But how can i add the values? for instance if i type: "I am new" - how can i make it to sum up all the values together ?

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Simple, just keep a running total through the loop:

int sum = 0;

for ( int i = 0; word[i] != '\0'; i++ ) {
  for ( int j = 0; alphabet[j] != '\0'; j++ ) {
    if ( word[i] == alphabet[j] )
      sum += values[j];
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanx again! I am sure that when u have grandchildren they will be proud about you for indeed u are a usefull person to humanity ;) by the way have u ever been in Greece?

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Not to my knowledge.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Not to my knowledge.

Well if u ever decide to come let me know!! Thanx again

jepapel
Newbie Poster
18 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You