Solved

Shuffle characters in a string

Posted on 2007-04-09
2
296 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

810 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