Jump to content

Jumbled letters in C

- - - - -

  • Please log in to reply
6 replies to this topic

#1
twogreenarrows

twogreenarrows

    Newbie

  • Members
  • Pip
  • 3 posts
Help, good day. i'm trying to make a program in c which basically jumble possible combinations of a given word for example

i input: CAR

RAC
ACR
CRA
...
..
..

How do i do this? Need help please.

#2
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
Here's one solution for that (pseudo code):
string InputString; //String that contains your input; in this case: CAR
array OutputBuffer[size of InputString];

variable a = 0;

while(all elements of InputBuffer != '$')
{
   variable n = random integer number between 0 and size of InputString;
   if(InputString[n] != '$') //$ is used to show the program that this element is already stored in OutputBuffer
   {
      OutputBuffer[a] = InputString[n];
      a++;
      InputString[n] = '$';
   }
}
Print(OutputBuffer);

Greets,
artificial

#3
twogreenarrows

twogreenarrows

    Newbie

  • Members
  • Pip
  • 3 posts
hmm i think this can be done the easy way using pointers can you sow me?

#4
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

twogreenarrows said:

hmm i think this can be done the easy way using pointers

I just said that this is one solution. Of course there are more and some of them might be better. ;)
Actually, my example uses pointers. In fact, a string is a pointer to the first char of an array that is zero terminated.

Greets,
artificial

EDIT: Tell me your basic idea. Then I (and the others) can give you concrete tips.

Edited by artificial, 24 June 2010 - 08:04 AM.
EDIT


#5
twogreenarrows

twogreenarrows

    Newbie

  • Members
  • Pip
  • 3 posts
Input:
car

possibilities:

dan
dna
nda
nad
and
adn
Thank You!

like that no functions, printf, scanf, just simple things okay thanks!

#6
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
Yeah, I understand what the program is supposed to do. You just have to swap the chars' locations until you have the desired result.
My code is one example for that (and indeed an inefficient one).
Maybe I just don't get it. I hope that the other members can help you. :)

Greets,
artificial

PS: I don't think that there's a reasonable function that creates "dan" from the word "car". ;)

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
This has already been answered here - String Permutation.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users