0

I've just started learning C this semester after primarily learning Java and I've been asked to create a program which allows the user to enter in a string, and then scramble the letters inside their string.

#include <stdio.h> //Alows input/output operations
#include <stdlib.h> //Standard utility operations

int main(int argc, char *argv[]) //Main method
{
  printf("------------------------\n");
  printf("WELCOME TO THE SCRAMBLER\n");
  printf("------------------------\n\n");

  char userString[50]; //Declaring the user string 
  printf("Please input a String :> "); //Iforms the user to enter a string
  scanf("%s", userString); //Allows the user to enter in a string

  char targetLetter[2]; //Declaring the target letter
  char replaceLetter[2]; //Declaring the replace letter

  while(1)
  {


  }
}

This is what I currently have, I just need help/advice on how to actually scramble the string. The user should be able to scramble the string as many times as they like, until they enter a specific character, then the program terminates. Thanks for any help in advance!

Ben Parry
  • 133
  • 1
  • 4
  • 19
  • 1
    If by scrambling you mean just shuffling the letters into a random order, you could use a [fisher-yates shuffle](http://stackoverflow.com/questions/3343797/is-this-c-implementation-of-fisher-yates-shuffle-correct). – indiv Mar 04 '15 at 16:16
  • @indiv Thanks for the reply. What I'm aiming for is so the user can select a letter from their created string and replace it with another letter/character. – Ben Parry Mar 04 '15 at 16:17
  • 1
    [This](http://www.stackoverflow.com/questions/8631023/are-there-any-algorithms-for-scrambling-a-word) will interest you. – Spikatrix Mar 04 '15 at 16:19

1 Answers1

-2

The first thing you want to do is think to a simple algorithm to shuffle the string.

(Note that scanf() & %s will read a string until a space is found after hitting enter. Therefore, 'hello world' would be just 'hello'. Instead use gets() if you need to acquire even space characters).

A simple shuffle function could be:

void mix (char * string) {
    //Get string length
    int len = strlen(string);
    int i;

    char tmp;

    for (i = 0; i < len / 2; i++) {
        //Loop every char till the middle of the string
        //Save the current char
        tmp = string[i];
        //Put in this position the opposite char in the string
        string[i] = string[len - i];
        //Replace the opposite char
        string[len - i] = tmp;
    }
}

//If you input hello you should get olleh

However this is not what you was exactly looking for, but I would suggest you to make two or three functions like this and in the while() loop keep calling them one by one.

Before the loop restarts just ask the user for a char with a scanf().

The while loop should be something like:

while (char != 'a') {
  //Mix the string
  mix_1();
  mix_2();
  mix_3();
  //Ask char again
  scanf("%c",&char);
  //Loop goes on till char != a
}

Hope this helped.In this case a simple vote on this answer would really be helpful for me.

Cheers

Alberto
  • 4,212
  • 5
  • 22
  • 36
  • 2
    No, use `fgets()` instead! – pmg Mar 04 '15 at 16:32
  • Even if it throws a warning, for his purposes he should definitely be fine. Thanks for the tip anyway – Alberto Mar 04 '15 at 16:33
  • Problem is he uses a deprecated function rather than a safe function he gets to be able to use into the future. – pmg Mar 04 '15 at 16:34
  • 2
    [Why the `gets()` function is dangerous and should never be used](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Mar 04 '15 at 16:41
  • Again, I'm not saying it is safe, but for this purposes will just be fine. I have been developing C programs for the past 6 months and have always used gets. That is why I suggested it. This said, I'm aware of the problems related to it – Alberto Mar 04 '15 at 16:43
  • 2
    @FoxNos: `gets()` is declared in ``, not ``. Unless your compiler conforms to the 2011 ISO C standard; then `gets()` is not declared anywhere (it was removed from the language because it's inherently unsafe). I urge you to stop using `gets`, and *please* stop advising others to use it. – Keith Thompson Mar 04 '15 at 18:37
  • "scanf() & %s will read a string until a space is found after hitting enter." is _somewhat_ correct but oversimplifies the true functionality. `"%s"` _begins_ by reading and discarding white-space, then it saves non-white-space `char` until eventually encountering white-space or `EOF`. It, like `gets()` is not buffer size protected and both should not be used. `fgets()` is a better approach. – chux - Reinstate Monica Mar 04 '15 at 19:21