473,388 Members | 1,574 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,388 software developers and data experts.

recursive functions of 1)strcmp 2)reverse a string

82
I need two recursive functions
1) the first must do the job of strcmp
I want to give me some tips about this such as example what am i supposed to compare the first character of each string and return the result? or all the characters? how strcmp works?

2)the second must print a string passed as an argument reverse
I make a try, but it does not print me the first character
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void rev(char *p);
  5.  
  6. int main(void)
  7.  char *k="ple";
  8.  rev(k);
  9.  
  10.  return 0;
  11. }
  12.  
  13. void rev(char *p)
  14. {
  15.  int l;
  16.  
  17.  l=strlen(p)-1;
  18.  
  19. if(l==0)
  20.   return;
  21.  else 
  22.   rev(++p);
  23.   printf("%c",*p);  
  24.  
  25. }
Sep 20 '07 #1
13 7136
Savage
1,764 Expert 1GB
I need two recursive functions
1) the first must do the job of strcmp
I want to give me some tips about this such as example what am i supposed to compare the first character of each string and return the result? or all the characters? how strcmp works?

2)the second must print a string passed as an argument reverse
I make a try, but it does not print me the first character
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void rev(char *p);
  5.  
  6. int main(void)
  7.  char *k="ple";
  8.  rev(k);
  9.  
  10.  return 0;
  11. }
  12.  
  13. void rev(char *p)
  14. {
  15.  int l;
  16.  
  17.  l=strlen(p)-1;
  18.  
  19. if(l==0)
  20.   return;
  21.  else 
  22.   rev(++p);
  23.   printf("%c",*p);  
  24.  
  25. }
1.How strcmp works.

How do you compare two strings?

If you find two strings that have different length,would you bother comparing each letter when there is NO WAY that those string would be the same?

I think that you can figure it out from this.

Now,after this(sorry,if I sounded rude) can you tell me how strcmp works?

Savage
Sep 20 '07 #2
kalar
82
1.How strcmp works.

How do you compare two strings?

If you find two strings that have different length,would you bother comparing each letter when there is NO WAY that those string would be the same?

I think that you can figure it out from this.

Now,after this(sorry,if I sounded rude) can you tell me how strcmp works?

Savage
Thanks but i think that strcmp don't compare the two strings with base the length of the strings.

edit:i find this about the return of strcmp
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2

I will make a try

Anyone about the second?
Sep 21 '07 #3
Savage
1,764 Expert 1GB
Thanks but i think that strcmp don't compare the two strings with base the length of the strings.
It compares each letter from one string with corresponding letter from the second string,but only if lengths are equal.

Savage
Sep 21 '07 #4
JosAH
11,448 Expert 8TB
It compares each letter from one string with corresponding letter from the second string,but only if lengths are equal.

Savage
No it doesn't, e.g. "b" > "aaaa" but the length of "b" is shorter than the length
of "aaaa".

kind regards,

Jos
Sep 21 '07 #5
kalar
82
I found about strcmp that works like a dictionary
any idea about 2?
Sep 21 '07 #6
Savage
1,764 Expert 1GB
I need two recursive functions
1) the first must do the job of strcmp
I want to give me some tips about this such as example what am i supposed to compare the first character of each string and return the result? or all the characters? how strcmp works?

2)the second must print a string passed as an argument reverse
I make a try, but it does not print me the first character
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void rev(char *p);
  5.  
  6. int main(void)
  7.  char *k="ple";
  8.  rev(k);
  9.  
  10.  return 0;
  11. }
  12.  
  13. void rev(char *p)
  14. {
  15.  int l;
  16.  
  17.  l=strlen(p)-1;
  18.  
  19. if(l==0)
  20.   return;
  21.  else 
  22.   rev(++p);
  23.   printf("%c",*p);  
  24.  
  25. }
Switch postiitons of rev(++p) and printf(..)

Savage
Sep 21 '07 #7
kalar
82
Switch postiitons of rev(++p) and printf(..)

Savage
this don't work
the code that i post prints the string reverse except the first character
Sep 21 '07 #8
this don't work
the code that i post prints the string reverse except the first character
Modify the code in line no.18 like this,

l = strlen(p);

instead of

l = strlen(p) - 1;
Sep 21 '07 #9
kalar
82
Modify the code in line no.18 like this,

l = strlen(p);

instead of

l = strlen(p) - 1;
thanks but also don't work
Sep 21 '07 #10
thanks but also don't work
Also, modify the last line as given below,

printf("%c",*(p-1));
Sep 21 '07 #11
kalar
82
Also, modify the last line as given below,

printf("%c",*(p-1));
yes worked but why *(p-1)?
Sep 21 '07 #12
Ganon11
3,652 Expert 2GB
Because you increment the pointer p in the function call, it now points to the second letter. You need to use *(p-1) to point it back to the char you wanted to print.
Sep 21 '07 #13
weaknessforcats
9,208 Expert Mod 8TB
It compares each letter from one string with corresponding letter from the second string,but only if lengths are equal.
Not true.

strcmp compares correspoding characters until there is a mismatch and one of the values is not a null terminator. If one is a null terminator, then if the other is also, the strings are equal. Otherwise, the difference between the first argument less the second argument is returned.

Keep in mind a scan of the entire string is required to get the length. In that amount of time the compare could have been done.

strcmp is about 5 lines of code.
Sep 21 '07 #14

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

Similar topics

64
by: dmattis | last post by:
I am trying to write a recursive version of Power(x,n) that works by breaking n down into halves(where half of n=n/2), squaring Power(x,n/2), and multiplying by x again if n was odd, and to find a...
7
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? ...
7
by: Michael | last post by:
Hi Everyone, I got the following code from Deborah Kurata's book. I need to return the control that matches the name that I pass into the function. It does iterate through the form but it will not...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
6
by: jeniffer | last post by:
Please give an example of 2 simple mutually recursive functions in C .
3
by: Babikie | last post by:
Write a program that performs a reverse recursion with following functions. void swop (char,int,int); void reverse (char); void rev(char,int, int); User should enter a string and all character...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
5
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.