473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

reverse a string with recursive function it is to be terminated by space not by enter

Hi all,

I prepared a program in c language to reverse a string using recursive function

In my program I terminated the string using new line character '\n'

But I want to change my program in the way that if a white space is pressed after typing a word then space bar ' ' should be used to terminate the string and string should be printed in reverse order

but when I used statement

Expand|Select|Wrap|Line Numbers
  1. if((ch=getchar())!=' ')
  2. rev();
then string is not being terminated after executing the program

here is the coding I used to apply my logic

Expand|Select|Wrap|Line Numbers
  1. void rev(void)
  2. {
  3. char ch;
  4.  
  5. if((ch=getchar())!='\n')
  6. {
  7. rev();
  8. printf("%c",ch);
  9. }
  10. }
  11.  
please guide me about the solution to this problem

Thank you
May 2 '11 #1
2 3236
weaknessforcats
9,208 Expert Mod 8TB
Well, you don't really have a string here. What you have is one character at each level of recursion.

To reverse a string, the easiest thing to do is swap the first and last characters of the string.

So in thiscase you call rev() with the address of the first and last character. When rev() calls rev() is does no by passing the addresses of the next character from the left and the previous character on the right.

rev() returns when the ttwo addressws ae equal.

And your string is reversed.
Now just display it in main().

You don't actually need delimiters. All you need is the address of the first and last character in the range of characters thest need reversing.
May 3 '11 #2
thank you for your valuable help
and I have also found a mistake in my logic to reverse a resies of entered characters actually I should have used getche() function in place of getch() then my logic is working . Thank You
May 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lugal | last post by:
I'm new to C++, coming from a Python background. I wrote the following code in C++ based on the Python code found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 ...
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
7
by: GysAnn | last post by:
Hello, I'm looking for a function for converting the input to a regular text. Example: when the input is "aBCdefgHI jkLM ooPP" the return value of the function should be "Abcdefghi Jklm...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
6
by: Uwe Grawert | last post by:
I have the following recursive function: string find_value_by_key (xmlDocPtr doc, xmlNodePtr root_node, const string& key) { while(root_node != NULL) { if(! xmlStrcmp( root_node->name,...
0
by: MrData | last post by:
Hi, I have a big problem with my recursive function. This function have the task to find all blocks in the template file out and put it recursive into a array. hierarchy of the blocks are...
4
by: hapa | last post by:
i was looking for questions on while loops when i came across a problem that said write a program that displays fibonacci numbers and the code that i wrote works just fine int x1=1; int...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.