• C

Shuffle characters in a string

Given a string, return the string with first and last chars of each word unchanged and rest chars jumbled up in any way.
For ex:
This is a String -> Tihs is a Srtnig

I have the following code to reverse a string: How can I modify it to return the above result?

#include < stdio.h >
#define SENT "This is a String"

int reverse(char *string, char delimiter)
{
    char *src, *dest,*temp=string;
    while( *temp ) {
         if (*temp == delimiter) {
                     temp++;
                     continue;
         }
         src=dest=temp;
         while ( (*(dest+1) != delimiter) && ( *(dest+1) != '\0' ) )
           dest++;
         temp=dest+1;
         while( dest > src ) {
                char temp = *dest;
                *dest-- = *src;
                *src++=temp;
         }
    }
}


int main()
{
    char name[] = SENT;
    printf("Before Call: %s\n",name);
    reverse(name,'\0'); /* Reverse Complete Sentence */
    reverse(name,' '); /* Reverse Words */
    printf("After Call: %s\n",name);
   
}
LVL 1
niftyhawkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi niftyhawk,

Given as a C# answer was given in another forum and that the question is not homework, here's a C solution that treats a space-like characters (blank, tab, etc.) as delimiters.

Note that the solutions can be based on recursion, iteration, or both.   :)

Kent


#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#define SENT "This is a String"

void WeirdReverse(char *string)
{
  char *ps;   // parameter start
  char *pe;   // parameter end
  char *next; // next starting location
  char temp;

  ps = string;
  while (1)
  {
    while (isspace (*ps))    // skip leading spaces
      ps++;
    if (*ps == 0)            // end of string (no parameter)
      break;
    for (pe = ps+1; *pe && !isspace(*pe) ;++pe);  // find the end of the word
    next = pe;   // save the next loop start
    --pe;        // set pe to the last character.  The loop above set it to last+1

    for (++ps, --pe; ps < pe; ++ps, --pe)      //  Swap characters to be reversed
    {
      temp = *ps;
      *ps = *pe;
      *pe = temp;
    }
    ps = next;
  }
}


int main()
{
    char name[] = SENT;
    printf("Before Call: %s\n",name);
    WeirdReverse(name);   /* Reverse Complete Sentence */
    printf("After Call: %s\n",name);
    return (0);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niftyhawkAuthor Commented:
Thanks a bunch for the answer and the code comments. I am able to understand the inbuilt logic clearly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.