473,498 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursion

momotaro
357 Contributor
this fonction is supposed to print character by character am just wondring if I can do better

Expand|Select|Wrap|Line Numbers
  1. void PROMPT(char *T)
  2. {
  3.     int i = 0;
  4.  
  5.     if(*(T+i) != '\0')
  6.     {
  7.         printf("%c", *(T+i));
  8.         i++;
  9.     }
  10.     else
  11.         return;
  12.     PROMPT(T+i);
  13. }
Nov 21 '09 #1
13 1915
YarrOfDoom
1,247 Recognized Expert Top Contributor
There is still some room for improvement.
Try going over this code line by line with some example input and see if you can find what is obsolete.
If you really can't find it, I could provide you with some more hints.
Nov 22 '09 #2
momotaro
357 Contributor
I feel that there is something odd because its not beautiful :) but can't get it
Nov 22 '09 #3
YarrOfDoom
1,247 Recognized Expert Top Contributor
Well to me the code looks like a converted loop (replace if by while and remove everything starting from else and it would work, however I guess you want to keep it recursive here), so try thinking about something you would need in a loop, but not necessarily in a function.
Nov 22 '09 #4
momotaro
357 Contributor
I know that that "i" is not a good idea but how do you wanna keep track of your position on the string??
Nov 22 '09 #5
YarrOfDoom
1,247 Recognized Expert Top Contributor
If you look closely at the code, you see that i only takes the values '0' and '1', because every time the function is called, it is initialised back to '0'.
So in this case, i can be replaced with constants throughout the code.
Nov 22 '09 #6
momotaro
357 Contributor
the code become:


Expand|Select|Wrap|Line Numbers
  1.  
  2. void PROMPT(char *T)
  3. {
  4.     if(*T != '\0')
  5.         printf("%c", *T);
  6.     else
  7.         return;
  8.     PROMPT(T+1);
  9. }
and it's working! thx
Nov 22 '09 #7
Banfa
9,065 Recognized Expert Moderator Expert
Your printf could be replaced with putchar.

Where you recursively call PROMPT ask yourself under what condition you get there? There are 2 return points from this function, they could be reduced to 1.
Nov 22 '09 #8
momotaro
357 Contributor
sorry but I don't see how
Nov 22 '09 #9
Banfa
9,065 Recognized Expert Moderator Expert
The function has 2 execution paths when *T != '\0' and when *T == '\0'. It also has 1 explicit return (and i implicit return). Obviously you can not remove the implicit return so examine the 2 execution paths to see which statements get executed for the 2 conditions and see if you can simplify the 2 paths and reduce to a single implicit return.

As a clue you need an if without an else.
Nov 22 '09 #10
momotaro
357 Contributor
Expand|Select|Wrap|Line Numbers
  1. void PROMPT(char *T)
  2. {
  3.     if(*T == '\0') 
  4.         return;
  5.     printf("%c", *T);
  6.     PROMPT(T+1);    
  7. }
thx!
Nov 22 '09 #11
Banfa
9,065 Recognized Expert Moderator Expert
Not bad but reverse the if condition and you can remove the return statement too, you will need 1 each of these {, } too.
Nov 22 '09 #12
momotaro
357 Contributor
perfect this is recursion ! thx
Nov 22 '09 #13
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. if(*T == '\0') 
is the same as

Expand|Select|Wrap|Line Numbers
  1. if(!(*T))  
so you get:

Expand|Select|Wrap|Line Numbers
  1. if(!(*T)) return; 
Nov 23 '09 #14

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

Similar topics

5
3395
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
2735
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
4115
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
1552
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
5537
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
2258
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
3698
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
4497
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
2957
by: athar.mirchi | last post by:
..plz define it.
35
4682
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.