Solved

Shuffle characters in a string

Posted on 2007-04-09
2
290 Views
Last Modified: 2012-08-14
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);
   
}
0
Comment
Question by:niftyhawk
2 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 18877309
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
 
LVL 1

Author Comment

by:niftyhawk
ID: 18877508
Thanks a bunch for the answer and the code comments. I am able to understand the inbuilt logic clearly.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now