5 Replies - 711 Views - Last Post: 05 April 2008 - 11:56 AM Rate Topic: -----

#1 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

does anyone know how to do this using c

Posted 04 April 2008 - 11:06 AM

does anyone know how to give the characters of a string numeric variables? so when i can print the numeric variables in decending order so the word is read backwards?
for example
hello h=1 e=2 l=3 l=4 0=5
olleh

your help would be very appreciated, a snippet would be awesome, or a written code would be very great.
Is This A Good Question/Topic? 0
  • +

Replies To: does anyone know how to do this using c

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: does anyone know how to do this using c

Posted 04 April 2008 - 11:09 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: does anyone know how to do this using c

Posted 04 April 2008 - 11:41 AM

Well this would not be the BEST way to reverse a word/string, but...

There are a number of ways of doing this (associating a number to each letter).

First of all there is the natural way. The index of the character in the array or string. This basically what people usually use.

Another way would be to use an associative construct such as a map. For example: map<int, char> would work. This would make it pretty easy to use the STL sort algorithm to do what you want.

You could create your own associative struct/class:

struct {
	int key;
	char letter;
} pair;


But if all of this is just to reverse a string you might want to use a more traditional method.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: does anyone know how to do this using c

Posted 04 April 2008 - 11:54 AM

If you want to reverse a string, try looking at this snippet

If that doesn't make sense, let me know and I'll try to clarify it for you :)
Was This Post Helpful? 0
  • +
  • -

#5 ladiesman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-February 08

Re: does anyone know how to do this using c

Posted 04 April 2008 - 10:17 PM

theres my code, i'm getting (NULL) inmy terminal when i run the program, but if i remove ca[5] i get buss error. can you help me fix it.
#include <stdio.h>

int main()
{
char ca[10];
ca[0] = 'H';
ca[1] = 'e'; 
ca[2] = 'l';
ca[3] = 'l';
ca[4] = 'o';
ca[5] = '\0';
printf("%s", ca[5],ca[4],ca[3],ca[2],ca[1],ca[0]);
getchar();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: does anyone know how to do this using c

Posted 05 April 2008 - 11:56 AM

Strings in C are (as you seem to understand) zero terminated. So if you did:

printf("%s", &ca[5]);

it would print out an empty l string "".

As for the rest of the arguments, you only told the printf() function to look for 1 value. I am pretty sure this messes things up and may cause the program to leave extra data on the stack when the function exits.

try putting the data into a new array:

output[0] = ca[4];
output[1] = ca[3];
output[2] = ca[2];
output[3] = ca[1];
output[4] = ca[0];
output[5] = '\0';

and then print the output string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1