## Recursive function to compare strings without library functions

c program to compare two strings without using strcmp
c program to compare two strings without using strcmp using pointers
c++ program to compare two strings without using strcmp
c++ program to compare two strings without using library function
string compare using recursion in c
write a program to read two strings and compare them whether they are identical(case insensitive).
compare two strings c without using strcmp
c program to compare two strings using strcmp

I'm supposed to write a recursive function in C programming language that checks if string 1 is greater than or equal or less than string 2, and thus returning `1`, `0`, `-1` respectively.

Below is my code that I've written. The program cannot terminate and I cannot figure out the reason. Please give me some advice. Thank you.

```int revisedStrcmp(char *s1, char *s2) {
int i = 0, n = 0, p = 0;

if (s1[i] == '\0' && s2[i] != '\0') //s1 shorter than s2
return -1;

else if (s1[i] != '\0' && s2[i] == '\0') //s1 longer than s2
return 1;

else if (s1[i] != '\0' && s2[i] != '\0')  //both not equal to null
{
if (s1[i] > s2[i])  n += 1; //s1
else if (s1[i] < s2[i]) p += 1; //s2
else
{
n += 1; //s1
p += 1; //s2
}
i += 1;
return revisedStrcmp(s1, s2);
}
else    //if s1[i] & s2[i] are null
{
if (n > p) //s1 > s2
return 1;
else if (n < p)
return -1;
else
return 0;
}
}
```

The main problem in your function is you do not pass updated pointers in the recursive call to `revisedStrcmp`, causing an infinite loop and a potential Stack overflow.

Here is a corrected and simplified version:

```int revisedStrcmp(const char *s1, const char *s2) {
if (*s1 < *s2)
return -1;
if (*s1 > *s2)
return +1;
// *s1 == *s2
if (*s1 == '\0')
return 0;
return revisedStrcmp(s1 + 1, s2 + 1);
}
```

There is no need to make special cases for shorter strings because the null terminator can be used in the comparisons.

This particular style of recursion is called tail recursion and will be compiled into a loop by modern compilers.

Note however that for `revisedStrcmp()` to return the same order as `strcmp`, the comparisons must be performed on `unsigned char` values instead of plain `char`, which can be signed by default on many architectures:

```int revisedStrcmp(const char *s1, const char *s2) {
unsigned char c1 = *s1, c2 = *s2;
if (c1 < c2)
return -1;
if (c1 > c2)
return +1;
// c1 == c2
if (c1 == '\0')
return 0;
return revisedStrcmp(s1 + 1, s2 + 1);
}
```

C Program to compare two strings with and without strcmp() Function, In this program we will compare strings using strcmp() function defined in the string.h library. strcmp(a, b) returns 0 if both the strings a and b are exactly same else� C Program to Compare Two Strings Without Using Library Function [crayon-5f190df84c8dd393845703/]

You are not passing to the function the `i, n, p` variables. In that way your function will not end, because it will start every time with the counter variables at 0.

Examples of Recursion: Recursion with the String Library, We'll start with the most basic of the string functions, the strlen() function, which of doing a string comparison on the rest of the string, so we recursively call our� String reversing without a library function is an important question.There is two method to reversing a string Iterative method and recursive method.

In the part:

```    else if (s1[i] !='\0' && s2[i] !='\0')  //both not equal to null
{
if (s1[i]>s2[i])  n+=1; //s1
else if (s1[i]<s2[i]) p+=1; //s2
else
{
n+=1; //s1
p+=1; //s2
}
i+=1;
return rStrcmp(s1,s2);
```

you call `rStrcmp(s1,s2);` with `s1` and `s2`, however, you have just processed a character. Call `rStrcmp(s1+1,s2+1);`.

Note: since your function processes only one character per call, you don't need to index the strings with `i`. It is always 0 except before a return statement, so its value is never used:

```int rStrcmp(char *s1, char *s2)
{
if (*s1 =='\0' && *s2 !='\0') //s1 shorter than s2
return -1;

else if (*s1 !='\0' && *s2 =='\0') //s1 longer than s2
return 1;

else if (*s1 !='\0' && *s2 !='\0')  //both not equal to null
{
if (*s1>*s2)  return 1; //s1
else if (*s1<*s2) return -1; //s2
else
return rStrcmp(s1+1,s2+1);
}
else    //if s1[i] & s2[i] are null
{
// since both are null, they are the same length and each
// character was the same: equal
return 0;
}
}
```

C Program to Compare Two Strings without using strcmp function , C Program to Compare Two Strings without using strcmp function. Here's simple C Program to Compare Two Strings without using library function in C Program.. Number Programs � String Programs � Arrays Programs � Functions Write a C Program to find frequency of vowels in String using Recursion. C Program to Compare Two Strings without using strcmp This program allows the user to enter two string values or two-character array. Next, this compare strings program will use For Loop to iterate every character present in that string and compares individual characters. I suggest you refer to strcmp function.

Just change the `return rStrcmp(s1, s2);` to `return rStrcmp(s1+i, s2+i);`. So that way you are storing the increments of your array position.

C program to compare two strings, Comparing two strings lexicographically without using string library functions. How to compare two strings using strcmp() library function. C Program to compare two strings using strcmp() Function. In this program we will compare strings using strcmp() function defined in the string.h library. strcmp(a, b) returns 0 if both the strings a and b are exactly same else returns -1. It is case sensitive so 'ABC' and 'abc' are considered to be different strings.

Reason is that `s1` and `s2` are the same in recursion. What I mean: if you have `char *s1 = "Hello";` and `char *s2 == Elloh`, your recursive calls are the same. you start at the same point, always, you do not pass increments in any way `(n, p)` so you are basically starting from same point every recursive call. What you can do is increment pointer and here is short solution:

```int revisedStrcmp (char *s1, char *s2) {
if (*s1 == *s2)
return *s1 == '\0' ? 0 : revisedStrcmp(s1 + 1, s2 + 1);

return (*s1 > *s2) ? 1 : -1;
}
```

or you can do following:

```return revisedStrcmp(s1+i, s2+i);
```

Program for length of a string using recursion, Given a string calculate length of the string using recursion. How to find length of a string without string.h and loop in C? Function to calculate length */. You can use strcmp (str1, str2) to compare two strings present in string.h header file. It returns -1 if first string is lexicographically smaller than second string, returns 0 if both string are lexicographically equal else returns 1 if first string is lexicographical greater than second string.

The strcmp() Function in C, This function compares strings character by character using ASCII value of the characters. The comparison stops when either end of the string� These are some of the functions in string.h for string operation. To use these functions you must include header file <string.h>. But we can make our own functions to perform above task without including string,h. Here is the complete source code that has own functions find_length (like strlen) to find the length, join_strings( like strcat) for

[PDF] Strings and Recursion, You will receive two scores: a functionality score and a style score. The following functions check whether a character is of a given In C++, the == operator can directly be used to compare strings: if (str1 == str2) { C++ strings, a library implemented in C++. ○ Any string Reversing a String Recursively. TOP reverse(". Write a menu-driven program in C to perform the following operations on string using standard library functions. i) Calculate Length of String ii) Reverse a given String iii) Concatenation of one string to another iv) Copy one String into another v) Compare two Strings Solution:

String compare in C, You can use do it using strcmp function, without strcmp function and using pointers. Function strcmp is case sensitive and returns 0 if both the strings are same. C� This function takes two strings and a number num as arguments and compare at most first num bytes of both the strings. num should be at most equal to the length of the longest string. If num is defined greater than the string length than comparison is done till the null-character(‘\0’) of either string.

• @chqrlie Is this what you meant? That is a good idea, thank you!!! In case where they are different yes it saves 2, but I also can get away with one test case less in other cases. What I mean is that I can get away with not testing if `*s2 == '\0'` because if '*s1 == *s2` and `*s1 = '\0'` it automatically means that `s2 = '\0'`