Solved

Shuffle characters in a string

Posted on 2007-04-09
9
3,577 Views
Last Modified: 2008-01-09
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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18876619
It looks like it's in the wrong section, this is very much C/C++.   Do you want this in C# though?
0
 
LVL 1

Author Comment

by:niftyhawk
ID: 18876671
A c# equivalent for the above would help too.. I have posted this in C/C++ and C# section.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18876789
In C/C++ and C# I would create complete diferent realization but the main Idea can be the same.
- Find the words.
- Swap symbols in word as needed.

In C I would create an additional array with start/end word positions.
In C# I would split a string into the words first.

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 5

Expert Comment

by:Yttribium
ID: 18876865
using System;
using System.Collections.Generic;
using System.Text;

namespace ShuffleLetters
{
      class Program
      {
            static void Main(string[] args)
            {
                  clsShuffleLetters c;
                  c = new clsShuffleLetters(args);
            }
      }


class clsShuffleLetters
{
      public clsShuffleLetters(string[] args)
      {
                  string strTemp = String.Empty;
                  string strFinal = String.Empty;

                  for (int i = 0; i < args.Length; i++)
                  {
                        strFinal += this.ShuffleWord(args[i].ToString()) + " ";
                  }

                  Console.WriteLine(strFinal);
                  string s = Console.ReadLine();
            }

            public string ShuffleWord(string s)
            {
                  Random r = new Random((int)(new TimeSpan()).Ticks);
                  System.Collections.ArrayList arl = new System.Collections.ArrayList();
                  string strFinal = String.Empty;
                  int temp = 0;

                  if (s.Length > 2)
                  {
                        for (int i = 1; i < s.Length - 1; i++)
                        {
                              arl.Add(i); //fill the index list
                        }

                        for (int c = 1; c < s.Length -1 ; c++)
                        {
                              temp = r.Next(arl.Count);
                              strFinal += s[(int)arl[temp]].ToString();
                              arl.RemoveAt(temp);                              
                        }
                        strFinal = s[0] + strFinal + s[s.Length-1];
                        return strFinal;
                  }

                  return s;

            }
      }
}

0
 
LVL 5

Accepted Solution

by:
Yttribium earned 500 total points
ID: 18876896
Commented Version:



using System;
using System.Collections.Generic;
using System.Text;

namespace ShuffleLetters
{
      class Program
      {
            static void Main(string[] args)
            {
                  clsShuffleLetters c;
                  c = new clsShuffleLetters(args);
            }
      }


class clsShuffleLetters
{
      public clsShuffleLetters(string[] args)
      {
                  string strFinal = String.Empty; //something to hold the sentence

                  for (int i = 0; i < args.Length; i++) //For each word
                  {
                        strFinal += this.ShuffleWord(args[i].ToString()) + " "; //Shuffle the middle part and stick it in a sentence
                  }

                  Console.WriteLine(strFinal); //Write it ... what else?
                  string s = Console.ReadLine(); //wait for input
            }

            public string ShuffleWord(string s)
            {
                  Random r = new Random((int)(new TimeSpan()).Ticks);  //just something to randomize
                  System.Collections.ArrayList arl = new System.Collections.ArrayList(); //a list that will hold a list of indexes (1 to the length of string-1)
                  string strFinal = String.Empty; //final string
                  int temp = 0; //dummy variable

                  if (s.Length > 3) //If the word is more than 3 letters, then shuffle (last one was 2, but 3 is also pointless)
                  {
                        for (int i = 1; i < s.Length - 1; i++) //Fill the array list with all the "shuffleable indexes"
                        {
                              arl.Add(i); //fill the index list
                        }

                        for (int c = 1; c < s.Length -1 ; c++) //For each letter between the first and the last
                        {
                              temp = r.Next(arl.Count); //get the first RANDOM index from our "usable index list"
                              strFinal += s[(int)arl[temp]].ToString(); //place the letter that's refenced in that index
                              arl.RemoveAt(temp);                              // remove this index, so we don't use it again
                        }
                        strFinal = s[0] + strFinal + s[s.Length-1]; // place the shuffled string between first and last letter and
                        return strFinal; //return it
                  }

                  return s; //nothing happened to the string, return it

            }
      }
}

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 18876954

Hey folks.

This is clearly a homework assignment.  Complete solutions are inappropriate.


Kent
0
 
LVL 1

Author Comment

by:niftyhawk
ID: 18876988
Thanks for the answers guys.. This is an interview question..not a homework question.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18876995
Well given that it was in C#, his homework appears to be in C++, so, I didn't mind , the functions I used in C# are NOT remotely usable in C++, he'd have to understand pointers, array access, etc.  Also it would need a little different approach in C++.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 18877046

Yeah.  I'm still not used to this auto-cross-posting/viewing thing in EE.  When in the "C" forum I still expect to see "C" questions.  The "C" forum has more student related questions so we police it quite a bit.

Apologies to all,
Kent
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

738 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