#### Archived

This topic is now archived and is closed to further replies.

# characters (integer value)

This topic is 5463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How would I make a program that takes the integer value of a character, and turns it into the character?

##### Share on other sites
You''ll have to cast the integer to a character.

  int num;// Get the number you wantcin>> num;// The (char) before num "casts" the integer to a character// So cout will treat the integer as a charactercout<< (char)num;

Is that what you wanted?

:::: [ Triple Buffer V2.0 ] ::::

##### Share on other sites
Yeah, cool thanks. I have yet another question. How do you print words in reverse in the following ways...

1)
Sentence is: "Hello, how are you?"
Reverse is: "?ouy era woh ,olleH"

and...

2)
Sentence is: "I am fine thanks"
Reverse is: "Thanks fine am I"

Any help would be much appreciated.

##### Share on other sites
er....sounds like a classic learning programming homework question. read the FAQ about homework and how you shouldn''t ask about it here. or explain why you need that for your game.

-me

##### Share on other sites
Excuse me? I''m in grade 10 and we learn TURING! TURING MAN!!!!!! Lol. I''m doing this because I want to learn, I''m making a scrambler. So... yeah

##### Share on other sites
That was me by the way... So, can any of you help me?

##### Share on other sites
programming problems are conceptual problems. if you are asked by someone to reverse a sentence, how do you do it?
quote:

Sentence is: "Hello, how are you?"
Reverse is: "?ouy era woh ,olleH"

Sentence is: "I am fine thanks"
Reverse is: "Thanks fine am I"

Tell me, how do you reverse those sentences? How do you know that the reverse version of "Hello, how are you?" is "?uoy era woh ,elloH" not something else? How do you know that "I am fine thanks" becomes "Thanks fine am I" not "Fine am thanks I"?

The way you do it is exactly the same way you are going to do it in your program.

500

##### Share on other sites
I have a major head-ache.... I dunno.... Can someone please tell me, I really want to get my app. working....

##### Share on other sites
char in[256];
char out[256];

int j = 0;

for(int index = strlen(in); index > 0; --index)
{
out[j] = in[index];
j++;
}

edit by ze: replaced "i" with "index" to avoid blind forum text replacement. Note to poster below: the array did have an index, you just didn't see it

[edited by - zealouselixir on March 4, 2003 8:07:54 PM]

##### Share on other sites
That wouldnt work because an array cant be = to an array. strcpy has to be used to copy an array but in thatcase I dont think it will let you copy a single character of the array. I think when you did out[j] = in; would mean that that one character of the string out would be set to the entire string of in rather than the right character. I did something about what you did except I just used one variable and it works fine.

#include <stdio.h>#include <string.h>int main( void ){	char string[80];	char *ptr;	int i;	printf("Enter a string:\n");	gets(string);	string[strlen(string)+ 1] = ''\0'';	ptr = string;	for(i=(strlen(string) -1);i>-1;i--)		printf("%c", string);	printf("\n");	return 0;}

##### Share on other sites
Hey grav, all I get is 0''s

##### Share on other sites
change:
printf("%c", string);
to:
printf("%c", string[ i ]);//<-page changes it to italics tag

[edited by - kordova on March 4, 2003 9:10:58 PM]

##### Share on other sites
gets() does set a ''\0'' on the end and I believe its fgets that doesnt do that. So thats why I set a NULL value to the end, so that wasnt necessary.

##### Share on other sites
If string[strlen(string)+ 1] = ''\0''; is taken out the for loop gets changed to

for(i=strlen(string);i>-1;i--)

When it added a space to the beginning that should have told me I didnt need to add the ''\0'' I was just too lazy to find out why it did it. Stupid mistake on my part.

##### Share on other sites
Awsome guys, thanks. How would I go about changing the inversed string into integers? I also can't get it to work properly when I try to type in that number into my other .cpp file (the decoder). Thanks.

        //decoder.cpp// This is the 'decoder'. It unsrambles the code. The encoder//(if I get it to work properly, right now it just converts the//string into integers without the string being first inversed)//will hopefully output some numbers that hold the value of the //characters, and when put into the 'decoder', will turn those //integers into characters once again, and inverse them.#include <iostream.h>#include <string.h>int main (void){	int num;	cout<<"Enter the numbers here:";	cin>>num;	cout<<(char)num;	return 0;}

[edited by - roble on March 4, 2003 11:32:03 PM]

[edited by - roble on March 4, 2003 11:34:11 PM]

[edited by - roble on March 4, 2003 11:34:41 PM]

[edited by - roble on March 4, 2003 11:35:07 PM]

##### Share on other sites
Did you have a question in that last post? I compiled that code you just posted and it seems to work unless you want it to do something other than display the character that the input number represents.

##### Share on other sites
Yeah I had one... There''s actually two parts to this:

In scramble.cpp

You know how I wanted to know how to reverse a string and all? Well, what I''m tring to do, is once that is reversed, I want to turn the character values of that reversed string into their integer values (With the integers representing the appropriate characters).

In descramble.cpp

I want to do the exact opposite. (Turning the integers back into the reversed string, and then reverse the string again so it is readible).

I actually have two questions now:

1) How do I turn the reversed string into the appropriate integer values?

and...

2) How do I create the syntax in "descramble.cpp"? (I know sort of the basis syntax, but get stupid little errors all the time).

##### Share on other sites
#include <stdio.h>#include <string.h>// Global variable so you can use it in other functions// *NOTE this holds 80 integers    int num[80];   // Same size of array int main( void ){	char string[80];	char *ptr;	int i;	printf("Enter a string:\n");	gets(string);	for(i=(strlen(string) -1);i>-1;i--){    printf("%c", string);    num = string[i]}printf("\n");// This loops through again and sends it back // using num[] in foward orderfor(i=0;i<(strlen(string));i++)    printf("%c", num[i]);printf("\n");return 0;}

##### Share on other sites
for(i=(strlen(string) -1);i>-1;i--)
should be
for(i=strlen(string);i>-1;i--)

and you dont need char *ptr;

##### Share on other sites
quote:
Original post by GravtyKlz
That wouldnt work because an array cant be = to an array.

This is true, but the AP wasn''t copying arrays. He was copying specific characters in each array, which is perfectly fine. I tested his code, and it didn''t work, but it only needs a little improvement.

quote:
Original post by GravtyKlz
strcpy has to be used to copy an array but in thatcase I dont think it will let you copy a single character of the array. I think when you did out[j] = in; would mean that that one character of the string out would be set to the entire string of in rather than the right character. I did something about what you did except I just used one variable and it works fine.

Sorry, you''re wrong. You haven''t been programming for very long, have you? I don''t mean to offend you, but be more careful when you dispense advice.

quote:
Original post by Roble
How would I go about changing the inversed string into integers?

Look up atoi() in the MSDN.

-Mike

##### Share on other sites
quote:

Sorry, you''re wrong. You haven''t been programming for very long, have you? I don''t mean to offend you, but be more careful when you dispense advice.

Which part was I wrong about? the strcpy part or out[j] = in? Im not offended at all, if someone cant take correction then they''ll never get better. I did say that "I think" though so I wasnt telling him for sure, but I should have looked into it more before I said anything. My apologies...

##### Share on other sites
When you say you want the character values converted to the integer values, do you mean the ASCII integer values or their placement in the alphabet. For example, do you want a=1, b=2, c=3, ..., or do you want a=0x61, b=0x62, c=0x63, etc. For the former, your loop would look like this:

// assumes all letter are lowercase. this works because
// the ascii code for ''a'' 61h
for(int i = 0; i < strlen(buffer); i++)
buffer -= 0x60

For the latter, your question would make no sense. The letters are already stored as their ASCII values. Your computer doesn''t store anything as letters, only numbers. If you want to print them out as numbers, just typecast them.

##### Share on other sites
Or try using an Enumerated type such as:

enum letters ("zero","a","b","c",..etc...)

if you are trying to get the letters to match their alphabet position.

I''m not positive if that syntax is 100% correct, but check into enums. I believe that 0 will be your first element so take that into consideration. I think in doing it this way, if you try to print "a" it will output 1. like I said I''m not 100% positive, but it might lead you in the direction you are trying to go.

Good Luck

##### Share on other sites
To help explain enums I grabbed this quick link off the web. It will give a very basic explanation of enums if you are not familiar with them

http://www.enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/

##### Share on other sites
Just wanted to know what all of these meant:
They''re in Printf() alot.
%c
%s
%d

etc.

and...

My source... Seems have 1 error in the for statement: Where it says count<(strlen(szInput)).

  #include <string.h>#include <stdio.h>#include <stdlib.h> int main (){  int count;  char szInput [256];  printf ("Enter a number: ");  gets ( szInput );//////////for(count=0;count<(strlen(szInput));count++)  printf("%c", atoi(szInput[ count ]));printf("\n");//////////  return 0;}

Thanks.