Is is possible to take an array full of characters and then make the characters into numbers?

i.e. a =1, b=2 etc.

I'm trying to write a program that deciphers ceasar shift ciphers.

Any ideas would be really helpful,

Thanks

dj_saxy

Recommended Answers

All 10 Replies

int get_code(char c)
{
  string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return static_cast<int>(alpha.find(c));
}

OK, i guess that it's a function so it should go at the start, but do i need to change anything, because my compiler doesn't like it...

Thanks for the help though

>because my compiler doesn't like it...
Well, I was assuming some measure of intelligence on your part. I won't make that mistake in the future:

#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int get_code(char c)
{
  string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return static_cast<int>(alpha.find(c));
}

int main()
{
  string s = "This is a test";

  cout<<"Code values of \""<< s <<"\" are:\n";
  for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
    if (isalpha(*it))
      cout<< left << setw(3) << get_code(*it);
  }
  cout<<endl;
}

U can define a function called as " convert "
th prototype will look like this : int convert( char)
for(i=0;i<MAX;i++)
number = convert(a);

int convert(char c)
{ return c - '0' ;
}

U can define a function called as " convert "
th prototype will look like this : int convert( char)
for(i=0;i<MAX;i++)
number = convert(a);

int convert(char c)
{ return c - '0' ;
}

So, do you just post random replies without reading the original question? Come to think of it, is this trick the only one you know? It seems like the only thing you post.

Consider the string ‘AAAABCCCCCDDDD’ consisting of alphabetic characters only.
This string is of length 14. Since the string consists of alphabetic characters only, duplicate characters can be removed and replaced with duplication factor n. With this technique the string can be compressed and represented by ‘4AB5C4D’.The compressed string is of length 7.
Write a program which takes a string in compressed form and recreates the original uncompressed string.

commented: Don't bump old threads -1
Member Avatar for iamthwee

Can you have something like:- 12A3K = AAAAAAAAAAAAKKK Where the number can be 2 digits or greater.

If not, it should be simple... loop through each char. But this is h/w so post your attempts.

Is is possible to take an array full of characters and then make the characters into numbers ?

To convert from 1 char to an int just subtract '0' from it. For example :

char ch = '1';
int i = ch - '0'; //i = int(1);

So just apply that to the whole array.

To convert from 1 char to an int just subtract '0' from it. For example :

char ch = '1';
int i = ch - '0'; //i = int(1);

So just apply that to the whole array.

So all chars to be possible are numbers (0x30 .. 0x39) only ?

Why only should chars be converted into integers, aren't they already integers (0x00 ... 0xff) internally?

-- tesu

So all chars to be possible are numbers (0x30 .. 0x39) only ?

Why only should chars be converted into integers, aren't they already integers (0x00 ... 0xff) internally?

-- tesu

Actually, I mis-read his post. Sorry.

commented: Naughty, naught :) -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.