| | |
How to assign my own numeric values to a char array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 18
Reputation:
Solved Threads: 0
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?
#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?
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)
#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] <<' '; } } }
New members chased away this month: 4
>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.
>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
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
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.
•
•
Join Date: Jul 2005
Posts: 18
Reputation:
Solved Threads: 0
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: class array problem!
- Next Thread: C++ from the WEB?!
Views: 10955 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







