473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Please help with basic recursive problem

2 New Member
Hi! I am very new to C programming. I have difficutly in understanding how the recursion work. One of my home work is to write a function using recursion to enter and display a string in reverse and state whether string contain any space - not to use array or string. I spent whole week but cannot list the space counted. Please help. Below is my code: (display string in reverse OK, but cannot count space:

#include <stdio.h>

void display(char);
int count(int);
int main(void)
{
char string;
printf("Enter a string:\n");
display(string);
printf("\n\n");
return 0;
}
void display(char ch)
{
int space;
ch = getchar();
putchar(ch);
if (ch != '\n')
display(ch);
putchar(ch);
Sep 28 '06 #1
2 1984
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. void display(char ch)
  2. {
  3.     int space;
  4.  
  5.     ch = getchar();
  6.     putchar(ch);
  7.     if (ch != '\n')
  8.         display(ch);
  9.     putchar(ch);
  10. }
  11.  
The is no need for ch to be a parameter of the function, you use it as a local variable only (i.e. the value passed in is not used)

Probably the easiest way to get the number of spaces would be to return it from display

Expand|Select|Wrap|Line Numbers
  1. unsigned display(void)
  2. {
  3.     char ch
  4.     unsigned space;
  5.  
  6.     ch = getchar();
  7.     putchar(ch);/* I think getchar echos the 
  8.                   character anyway so this might be unrequired*/
  9.  
  10.     if (ch != '\n')
  11.     {
  12.         space = display(ch);
  13.     }
  14.     else
  15.     {
  16.         space = 0;
  17.     }
  18.  
  19.     putchar(ch);
  20.  
  21.     if (ch == ' ')
  22.     {
  23.         space++;
  24.     }
  25.  
  26.     return space;
  27. }
  28.  
Sep 28 '06 #2
freshman
2 New Member
Thank you very much for your solution, i appreciated.
Sep 28 '06 #3

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

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
4
by: Hank | last post by:
I have two SQL Server 2000 machines (server_A and server_B). I've used sp_addlinkedserver to link them both, the link seems to behave fine. I can execute remote queries and do all types of neat...
4
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside...
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. ...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
17
by: marks542004 | last post by:
Hi all , I am an old programmer now hobbyist who has used cobol, basic, and RPG . I am new to c++ but have Microsoft Visual Studio 6.0. I have a program in Basic that reads a file , looks for...
4
by: rumbylove | last post by:
Could I please have the solutions to the following problems 1. (4 points) Your friend has to write code to make sure that the parentheses in an arithmetic expression are balanced, i.e. for every...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
0
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.