-4

Let me clear you first that I'm not a college student and this is not my home assignment. I am just curious to know the solution of this question which was once asked to me. I think this is a nice and tricky question which I feel worth sharing.The question was--

How do you input a string(said in general sense, independent of programming) from a user and print reverse of it in C/C++ without using array or any library function for reversing the user input?

I am unable to break-into this. Help please

Note: Members are marking it as a duplicate for this question. But All answers to this are either using library functions or using a pointer to char array(char *). None of them is allowed in my case. Please review it once again

Community
  • 1
  • 1
Asad
  • 1,817
  • 16
  • 23
  • 5
    so, have you tried something? – Keith Nicholas Jun 26 '13 at 05:06
  • 5
    This is not very tricky, and I'd be very surprised to find that it hasn't been asked before. – Jonathan Leffler Jun 26 '13 at 05:06
  • Are you allowed to recreate ("paraphrase") the library functions? – Jules Jun 26 '13 at 05:06
  • 1
    @Keith Nicholas: Unfortunately, my every solution starts with an array. How can I store a whole string without a char array. – Asad Jun 26 '13 at 05:11
  • @Jonathan Leffler: It seemed tricky to me, may be I am a little dumb, But please provide a solution. – Asad Jun 26 '13 at 05:15
  • @Jules Mazur: what do you mean by recreate library functions? – Asad Jun 26 '13 at 05:15
  • 2
    @Asad: Your question has been marked as a duplicate. There are a number of answers in the linked question that answer this problem. If those answers do not answer your question, could you elaborate as to __why__ they do not. Otherwise, you're going to receive a number of answers similar to that question, and similar to the answers below. – Bill Lynch Jun 26 '13 at 05:16
  • Using pointers: `void reverse_string(char *str) { char *ptr = str; while (*ptr != '\0') ptr++; while (str < --ptr) { char t = *ptr; *ptr = *str; *str++ = t; } }` or using array indexes: `void reverse_string(char str[]) { int i = 0; while (str[i] != '\0') i++; int j = 0; while (j < --i) { char t = str[i ]; str[i] = str[j]; str[j++] = t; } }` In both cases, it would be better to use `strlen()` in place of the first loop, but you said 'no library functions'. – Jonathan Leffler Jun 26 '13 at 05:23

2 Answers2

4

You can try recursion.

void print_reverse_str() {
  int c = getchar();
  if (c != EOF) {
    print_reverse_str();
    putchar(c);
  }
}
ruds
  • 786
  • 4
  • 8
0

Technically this is impossible because a string is a char array in c and an object representing a char array in c++.

I hope you meant not using arrays directly.

So try this pointer based solutions :

void strrev(char *str)
{
        if( str == NULL )
                return;

        char *end_ptr = &str[strlen(str) - 1];
        char temp;
        while( end_ptr > str )
        {
                temp = *str;
                *str++ = *end_ptr;
                *end_ptr-- = temp;
        }
}
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • I think *str here points to a char array. But my question is looking for a solution without using array. – Asad Jun 26 '13 at 05:19
  • 2
    That's silly, @Asad; a string _is_ an array of characters with a null terminator, by definition. The C standard says _'A string is a contiguous sequence of characters terminated by and including the first null character.'_ That means it is an array of characters. So you can't avoid using an array — though you can avoid using array notation (and Rohit's answer does almost completely, and could be made to do so completely). – Jonathan Leffler Jun 26 '13 at 05:27
  • @JonathanLeffler: I am totally agree with you. But please look at the **ruds** post. He is using recursion to both getting user input and outputting reversed string without explicitly using char array. – Asad Jun 26 '13 at 05:59
  • 1
    @Asad: But that answer is not reversing a string. It is outputting the input characters in reverse order, which is a different operation from reversing a string. At no time is [ruds](http://stackoverflow.com/users/66509/ruds) using a string at all. You can't meaningfully do input or output without using library functions, which your question specifically says are not allowed. Your terminology may be confused, but in C, 'string' has a precise meaning (previously quoted), and trying to use it for some other purpose merely leads to arguments. – Jonathan Leffler Jun 26 '13 at 06:16
  • I have used string in general sense. My Mistake. And I mean no library function to reverse the string. Of course! you can use it for I/O – Asad Jun 26 '13 at 06:33
  • 1
    @Asad: note that the recursive solution requires vastly more memory than a solution using a string. Of course, that won't matter for normal sized files (kilobytes or small numbers of megabytes), but it would matter for larger files (large numbers of megabytes, or gigabytes and beyond). – Jonathan Leffler Jun 26 '13 at 06:38
  • @JonathanLeffler- Will said & quoted. Thank you! – Rohit Vipin Mathews Jun 26 '13 at 09:11
  • @Asad- You can go ahead with the recursion but there is never a *string* in it. I have stated clearly at top about my solution. – Rohit Vipin Mathews Jun 26 '13 at 09:12