[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Shuffle characters in a string

Posted on 2007-04-09
2
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

649 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