Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

string[] myString = {"a","b","c","d"}
//Reverse string algorithm here
myString = {"d","c","b","a"}
I have been asked to do so in an interview without the help of any temporary variable or .NET class, string methods, etc to reverse the elements of the same string array. I was told to use basic programming constructs like loops. Since, I am up for another interview today, I am in a hurry to know whether this is actually possible because I could not find a solution to this.

share|improve this question
2  
We're not here to do your job interview for you. At least show some things you've tried. – Magnus Grindal Bakken Sep 6 '13 at 9:15
    
You were asked to store the values in reverse or update the array? – Nisarg Shah Sep 6 '13 at 9:15
    
I'm pretty sure there would be no downvotes if OP never metioned the job interview... – S_F Sep 6 '13 at 9:16
    
@MagnusGrindalBakken We will not be solving this for his interview. It will be for our knowledge or if we have then we can share with him – Nisarg Shah Sep 6 '13 at 9:16
    
@NisargShah Note the second part of my comment. There's nothing wrong with asking for help about school problems, job interviews, or work projects, but you have to show that you've made an attempt. – Magnus Grindal Bakken Sep 6 '13 at 9:17

12 Answers 12

up vote 6 down vote accepted

Here you go, no support variables, no .net functions :) But it makes the assumptions that all the strings in the array have length 1 ( as they do in the code you posted).

string[] myString = {"a","b","c","d", "e"};
for(int i = 0; i < myString.Length/2; i++)
{
   myString[i] += myString[myString.Length -1 -i];
   myString[myString.Length -1 -i] = ""+myString[i][0];
   myString[i] = "" + myString[i][1];
}
share|improve this answer
1  
That seems like a ridiculous assumption. If they're all length 1, the logical thing to do would be to have a char[] instead. – Dukeling Sep 6 '13 at 12:10
1  
@Dukeling If we were in the logic domain, you wouldn't be asked not to use .net funcions or accessory variables. I tried to solve the ask just as he presented it. – Save Sep 6 '13 at 12:12
    
The example is just that, an example. The description doesn't mention that we're limited to length 1 strings and it seems like a very strange constraint to put on strings, so my logical assumption is that the example only uses length 1 strings because that's less typing than longer strings. Yes, I'd be a lot happier with a wild-card-based approach. – Dukeling Sep 6 '13 at 12:18
    
Thanks @Save I was asked and therefore, asked for elements of length 1 in a string array. So it works and answers my question. – lbrahim Sep 6 '13 at 14:30

Since you cannot use a temporary variable, the best I can think of is appending the strings first and then removing the appended part again:

// append last strings to first strings
for(int i = 0; i < myString.Length / 2; i++)
{
    myString[i] = myString[i] + myString[myString.Length - i - 1];
}

// copy first half to last half
for(int i = myString.Length / 2 + 1; i < myString.Length; i++)
{
    myString[i] = myString[myString.Length - i - 1]
                      .SubString(0, 
                              myString[myString.Length - i - 1].Length 
                               - myString[i].Length);
}

// remove useless part from first half
for(int i = 0; i < myString.Length / 2; i++)
{
    myString[i] = myString[i].SubString(
                                  myString[myString.Length - i - 1].Length 
                                   - myString[i].Length);
}

Stupid approach? Yes. But no additional variables are involved.

share|improve this answer
    
As stated: without the help of any temporary variable or .NET class, string methods.. -> SubString usage ;-( – Heslacher Sep 6 '13 at 9:35
    
@Heslacher that's no .NET, that's the SubString that I implicitly implemented myself but just did not show ;) – Vincent van der Weele Sep 6 '13 at 9:36
    
Ha ha ha ;-) Show it and i will vote for it. – Heslacher Sep 6 '13 at 9:37
    
@Heuster I don't think SubString is implementable without temporary variables :-) – xanatos Sep 6 '13 at 9:53
    
@xanatos I'm afraid so too. But neither is string concatenation since strings are immutable. The question would make more sense to me for an array of integers (so the XOR trick could be used). – Vincent van der Weele Sep 6 '13 at 9:57

Sorry I posted a wrong answer... here's the verified one:

     int k = len - 1;
    for(int i = 0; i<len/2; i++)
    {
        myString[i] = myString[i]+"."+myString[k--];
    }
    for(int i = len/2; i<len; i++)
    {
        myString[i] = myString[k].substring(0, 1);
        myString[k] = myString[k--].substring(2,3);
    }

However, just consider this a pseudo-code... I did not check for .NET syntax.

share|improve this answer

This is not a complete answer, perhaps an idea..

It is possible to swap two numbers by mathematics

swap(a,b)
   a = a + b
   b = a - b
   a = a - b

Initially I was going to suggest this can be done with character ascii values.. Then I noticed the strings.

Is it acceptable then to

swap(str1, str2)
    str1 = str1+str2
    str2 = str1[0]
    str1 = str1[1]

I hope you get the idea

share|improve this answer

Have you tried this

        string[] myString = { "a", "b", "c", "d","e","f" };

        int _firstcounter = 0;
        int _lastcounter = myString.Length-1;

        while (_firstcounter<=_lastcounter)
        {
            myString[_firstcounter] += myString[_lastcounter];
            myString[_lastcounter] = "" + myString[_firstcounter][0];
            myString[_firstcounter] = "" + myString[_firstcounter][1];

            _firstcounter++;
            _lastcounter--;
        }
share|improve this answer

If the maximum string length is of array is less than 10, then is might be helpful....

string[] myString = { "aaaa", "bbb", "ccccccc", "dddddd", "e", "fffffffff" };
for (int i = 0; i < myString.Length; i++)
{
    myString[i] = myString[i].Length.ToString() + myString[i]; 
}
for (int i = 0; i < myString.Length/2; i++)
{
    myString[i] = myString[i] + myString[myString.Length-1-i];
    myString[myString.Length - 1 - i] = myString[i];
}
for (int i = 0; i < myString.Length/2; i++)
{
     myString[i] = myString[i].Substring(int.Parse(myString[i][0].ToString())+2,int.Parse(myString[i][int.Parse(myString[i][0].ToString())+1].ToString()));                       
}
for (int i = myString.Length / 2; i < myString.Length; i++)
{
     myString[i] = myString[i].Substring(1, int.Parse(myString[i][0].ToString()));
}
share|improve this answer

Try this.

public static class ExtensionMethods
{
    public static IEnumerable<T>  ReverseArray<T>(this T[] source)
    {
        for (int i = source.Length - 1; i >= 0; i--)
        {
            yield return source[i];
        }
    }

    public static T[] EnumerableToArray<T>(this IEnumerable<T> source)
    {
        var array = new T[source.Count()];

        int k = 0;
        foreach (var n in source)
        {
            array[k++] = n;
        }

        return array;
    }
}

Example usage

    public static void Main(string[] args)
    {
        string[] myString = {"a", "b", "c", "d"};

        myString = myString.ReverseArray().EnumerableToArray();
    }
share|improve this answer

Yes you can do it-

string[] myString = { "a", "b", "c", "d" };
myString  = (from a in myString orderby a descending select a).ToArray();
share|improve this answer
    
this is linq and also .net way of doing it – Nisarg Shah Sep 6 '13 at 9:28

You need at least one variable to swap values.

In pseudo code:

for(i : 0..n/2) {  
    // swap s[i] and s[n-i]
    String tmp = s[i];
    s[i] = s[n-i];
    s[n-i] = tmp;
}
share|improve this answer
1  
You use tmp variable in your solution. You just hide it making the code ugly and less efficient... – Jean Logeart Sep 6 '13 at 11:46
string[] myString = {"a","b","c","d"};
string[] reversed = new string[myString.Length];

for(int i = 0; i<myString.Length; i++) {
    reversed[i] = myString[myString.Length -i-1];
}
myString = reversed;
share|improve this answer
1  
'reversed' is a temporary variable. – metsburg Sep 6 '13 at 9:28

We Can use Array.Reverse(UrArray);

share|improve this answer
    
This will not work if UrArray is String[] – DanielV May 6 '15 at 8:47
    
cannot convert from 'System.Collections.Generic.List<string>' to 'System.Array' – DanielV May 6 '15 at 8:52
    
it will not work, definately – Juanito Jun 4 '15 at 9:54

Check out Array.Reverse method on MSDN.

share|improve this answer
1  
He wants to do it without using any existing methods – Microsoft DN Sep 6 '13 at 9:17
    
OUt of question. It is from .net – Nisarg Shah Sep 6 '13 at 9:17
    
ah, without .NET classes. Ah well, I'll leave the answer for future reference. – Hemario Sep 6 '13 at 9:18
    
It will not work and throw: cannot convert from 'System.Collections.Generic.List<string>' to 'System.Array' – DanielV May 6 '15 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.