niftyhawk
asked on
Shuffle characters in a string
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);
}
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);
}
It looks like it's in the wrong section, this is very much C/C++. Do you want this in C# though?
ASKER
A c# equivalent for the above would help too.. I have posted this in C/C++ and C# section.
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.
- 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.
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].T oString()) + " ";
}
Console.WriteLine(strFinal );
string s = Console.ReadLine();
}
public string ShuffleWord(string s)
{
Random r = new Random((int)(new TimeSpan()).Ticks);
System.Collections.ArrayLi st arl = new System.Collections.ArrayLi st();
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;
}
}
}
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[]
{
string strTemp = String.Empty;
string strFinal = String.Empty;
for (int i = 0; i < args.Length; i++)
{
strFinal += this.ShuffleWord(args[i].T
}
Console.WriteLine(strFinal
string s = Console.ReadLine();
}
public string ShuffleWord(string s)
{
Random r = new Random((int)(new TimeSpan()).Ticks);
System.Collections.ArrayLi
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;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey folks.
This is clearly a homework assignment. Complete solutions are inappropriate.
Kent
ASKER
Thanks for the answers guys.. This is an interview question..not a homework question.
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++.
Yeah. I'm still not used to this auto-cross-posting/viewing
Apologies to all,
Kent