Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Shuffle characters in a string

Posted on 2007-04-09
2
Medium Priority
?
319 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 46

Accepted Solution

by:
Kent Olsen earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

580 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