Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Read a string and print it in reverse without storing in an array using recursive
method OR non-recursive method.
Description:
Read a string from user.
Without modifying that string, print it in reverse order.
Implement in both recursive and non-recursive methods.
Pr-requisites:-
• Recursion
Objective: -
• To understand the concept of
◦ Recursion
Inputs: -
String
Sample execution: -

Enter a string
: Hello World
Reverse string is
: dlroW olleH
note: without using reverse function and use switch case to do this use your own function dont use strlen, reverse function or any function create your own function

What I have tried:

i tried this
C++
#include<stdio.h>
#include<string.h>
void reverse(char *);
int main()
{
    int option;
    char str[100];
    printf("Choose the option:\n");
    printf("1.Iterartive method\n2.Recursion method\n");
    scanf("%d", &option);
    switch (option)
    {
    case 1:
	    printf("Enter the string: ");
	    scanf("\n%[^\n]", str);

	    reverse(str);
	    printf("%s\n", str);
	    break;
    }
    return 0;

}
void reverse(char *str)
{
    int i,j;
    char ch;
    for (j = 0; *(str + 1) != '\0'; j++)
    {
	break;
    }
    printf("%d", j);
    for (i = 0; i < j/2; j++)
    {
	ch = *(str + i);
	*(str + i) = *(str + j - 1 - i);
	*(str + j - 1 - i) = ch;
    }
}
Posted
Updated 4-Nov-19 5:50am
v3
Comments
F-ES Sitecore 4-Nov-19 11:28am    
Asking other people to do your work isn't creating your own function, it isn't demonstrating you understand recursion, and (most importantly) it won't teach you anything.
Member 14644231 4-Nov-19 11:31am    
ok i need help i try my own but stuck in function send your email i will share you

Ask your self: what is a string?
That's pretty simple: it's a sequence of characters terminated by a null.
So all you have to do is find the null, counting the characters. When you know the length, you can print it in a simple loop by working backwards from the character at index length minus one, then length minus two, and so on. That's the non-recursive method.

The recursive method is even simpler (but seriously unnecessary):
Write a method that takes the string and and index. if the character at the index isn't null ('\0') then call yourself again, passing the array and the index plus one.
When you return, print the character at the index.

Think about it: this really isn't a difficult task (but the recursive one is more complicated and very unnecessary)
 
Share this answer
 
Comments
CPallini 4-Nov-19 15:23pm    
5.
Try to replace
C++
for (j = 0; *(str + 1) != '\0'; j++)

with
C++
for (j = 0; *(str + j + 1) != '\0'; j++)


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 4-Nov-19 15:24pm    
5.
Patrice T 4-Nov-19 15:27pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900