AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: Johnny Smartass — October 6, 2006
    +0 votes
      + -

    strrev(string_to_reverse);

  2. Submitted By: Johnny Smartass — October 6, 2006
    -1 votes
      + -

    I actually gave the answer above to a MS interviewer. He was not impressed.

  3. Submitted By: xianfeng — October 6, 2006
    -5 votes
      + -

    void rev(char str[], int len)
    {
    if (len>0)
    {
    printf(”%c”,str[len-1]);
    rev(str,len-1);
    }
    }

  4. Submitted By: smnaha — October 6, 2006
    +7 votes
      + -

    void strrev (char *s)
    {
    char *t = s + strlen(s) - 1;
    char ch = ‘’;

    while (s < t) {
    ch = *s;
    *s = *t;
    *t = ch;
    s++;
    t–;
    }

    }

  5. Submitted By: JV — October 6, 2006
    not yet rated
      + -

    by the end of the process, the address os s would have changed and you won’t get the correct answer. the solution would be to use a char* variable.
    ie.. add the following sentense before the while loop
    char *res=s;
    and return res

  6. Submitted By: christothes — October 6, 2006
    +8 votes
      + -

    this method uses only 2 bytes of memory

    revstr(char* str)
    {
    char* a = str;
    char* b = str+ (strlen(str) - 1);

    while(a < b)
    {
    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
    ++a;
    –b;
    }
    }

  7. Submitted By: arunkumar — October 6, 2006
    +0 votes
      + -

    well the above solution is a goog one.If you are thinking of reversing a string using recursion i think this is the solution.

    reverse( char *s)
    {
    static int i=0;
    while( *s != ‘/0)
    {
    if (i==0)
    {
    reverse(*s);
    i++;
    }
    else
    reverse(++s*);
    }/* end of while loop*/
    putchar(*s);
    }

  8. Submitted By: johny_bravo — October 6, 2006
    +2 votes
      + -

    revstr(char* s)
    {
    int len=strlen(s);
    int i;
    char t;

    for (i=0; i<len/2; i++) {
    c = s[i];
    s[i] = s[len - i];
    s[len - i] = c;
    }
    }

  9. Submitted By: iamsuechoi — October 6, 2006
    not yet rated
      + -

    JV’s idea is wrong.
    The address is in a local variable of a callee method.
    So it doesn’t change the address in a caller.

  10. Submitted By: rohitg — October 6, 2006
    not yet rated
      + -

    char* strRev( char* pStr )
    {
    char *pStart = pStr;
    char *pEnd = pStr;
    char c;

    while( *pStr++ );
    pStr -= 2;

    while ( pEnd < pStr )
    {
    c = *pEnd;
    *pEnd++ = *pStr;
    *pStr — = c;

    }

    return pStart;
    }

  11. Submitted By: Borcar — October 6, 2006
    +1 votes
      + -

    str_rev(char s[])
    {
    int len = strlen(s);
    int i;
    char temp;

    for(i=0; i<len/2; i++)
    {
    temp = s[i];
    s[i] = s[(len-1) - i];
    s[(len-1) - i] = temp;
    }
    }

  12. Submitted By: nandi — October 6, 2006
    -2 votes
      + -

    void main()
    {
    char ch;
    if( (ch = getchar()) != ‘n’ )
    main();
    printf(”%c”, ch);
    }

  13. Submitted By: doshimun — October 6, 2006
    +1 votes
      + -

    The function “Reverse” is called with the start address of the string to be reversed and number of characters to be reversed. This is useful because this function can also be used to reverse a portion of a string instead of an entire string, and such a function can also be used in “reversing words in a sentence”, another famous question from Microsoft.

    void Reverse(char * S, int N)
    {
    char * Start=S;
    char * End=&S[N-1];

    char Temp;

    while (Start<End)
    {
    Temp=*Start;
    *Start=*End;
    *End=Temp;
    Start++;
    End–;
    }
    }

    To reverse a string make a call this way:

    int main()
    {
    char Str[100]={”This is a sample string”};

    // Reverse entire string
    Reverse(Str, strlen(Str));

    // To reverse the first word only
    Reverse(Str, 4);

    // To reverse the fourth word only
    Reverse(Str+10, 6);
    }

  14. Submitted By: Piyush — October 6, 2006
    not yet rated
      + -

    void str_rev(char *s)
    {
    char *p;
    int i;

    for (i=0;*(s+i) != ‘’ ;i++ ){}
    p = (s+i);

    for (;i >= 0 ;i– )
    {
    putchar(*p);
    p–;
    }
    }

  15. Submitted By: rdakka — October 6, 2006
    -1 votes
      + -

    This is not string reverse, but it prints the input string in the reverse order.
    void rev( char *s)
    {
    if (*s) rev(s+1), cout << *s ;
    }

  16. Submitted By: rdakka — October 6, 2006
    +1 votes
      + -

    Here is the recursive version of rev method.
    #include <iostream.h>
    char* rev( char *s, char* t = 0)
    {
    if (!t) t = s;
    if (*s)
    {
    char* r = rev(s+1, t);
    if (r < s) *r ^= *s ^= *r ^= *s;
    return r+1;
    }
    return t;
    }
    void main()
    {
    char even_str[] = “Hell”;
    char odd_str[] = “Hello”;
    char null_str[] = “”;
    rev(even_str); cout << even_str << ‘n’;
    rev(odd_str); cout << odd_str << ‘n’;
    rev(null_str); cout << null_str << ‘n’;
    }

  17. Submitted By: sstirling — October 6, 2006
    not yet rated
      + -

    Here’s a C# solution:

    using System;

    namespace Reverser {

    public class StringReverse {

    static String reverse(String s) {
    String result = null;
    for(int i = 0; i <= s.Length; i++) {
    if (i > 0)
    result += s[s.Length - i];
    }
    return result;
    }

    public static void Main(String []args) {
    String test = “Reverse this String!”;
    Console.WriteLine(”Output: ” + reverse(test));
    }
    }
    }

  18. Submitted By: simonm — October 6, 2006
    not yet rated
      + -

    Using C#, you should rather use a StringBuilder with a fixed size of the original string. It’s far better in termes of perf and memory

  19. Submitted By: gbajekal — October 6, 2006
    not yet rated
      + -

    Here is a Java Solution

    public String reverseString(String str, int n)
    { String temp = “”;

    if( n >= str.length() -1)
    return temp + str.charAt(str.length() -1);// basis

    return reverseString(str, n + 1) + str.charAt(n); // recursion

    }

  20. Submitted By: Omer — October 6, 2006
    not yet rated
      + -

    Recursive solution:

    char * StrRev(char * s)
    {
    char t;
    if(strlen(s) <=1)
    return s;

    t = *s;
    strcpy(s,StrRev(s+1));
    *(s+strlen(s)) = t;

    return s;
    }

  21. Submitted By: k3xji — February 13, 2007
    not yet rated
      + -

    Yet another…

    char * revStr(char *s){
    int len = 0;
    char *rr = NULL;
    while(*s){len++;s++;}
    rr = (char*)malloc((len+1)*sizeof(char));
    rr[len] = ”;
    while(*rr){
    *rr = *–s;
    rr++;
    }
    return rr - len;
    }

  22. Submitted By: Swapnil Dipankar — August 16, 2007
    not yet rated
      + -

    while(index_low

  23. Submitted By: bambam — December 20, 2007
    not yet rated
      + -

    //string to hold letters
    char cString[8] = “reverse”;

    //iterators for the reverse
    int iBegin = 0;
    //this int has to hold the number of characters in the word.
    //use strlen or something, there are loads
    //out there, i’m just trying to keep this simple
    int iEnd = 6;

    //the numbers should not cross becuase your swapping
    //the front letters with the back, if the numbers meet
    //then you have an odd number of characters and the
    //middle one should stay the same :)
    while((iBegin > iEnd;

  24. Submitted By: Manjesh — February 14, 2008
    not yet rated
      + -

    function string_reverse(char str[n])
    {
    i = 0;
    j = n-1;

    while(i less than j)
    {
    swap(str[i++], str[j–]);
    }
    }

  25. Submitted By: tik — April 12, 2008
    not yet rated
      + -

    Copy from somewhere,

    include “string”

    string strRev( const string & s ) {
    return (s.size()==1 ? s : strRev(s.substr(1)) + *s.begin());
    };

  26. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.