tyuui112
tyuui112

Reputation: 27

Compare character by character 2 strings

I am trying to code a program that tells me if 2 strings are identcal. If they have one different chracter they are not.

I have this code, but it does not work, why?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
   char str1[30], str2[30];
   int i;

   printf("\nEnter two strings :");
   gets(str1);
   gets(str2);

for (i=0;str1[i]==str2[i];i++)
    if (str1[i]!=str2[i]){
        printf("They are not identical");
    }
    else continue;
    return (0);
}

It compiles with 0 errors and 0 warnings, but when I introduce 2 not identical strings it returns nothing. (The same happens when I introduce 2 identical strings, but that is how is supposed to be)

What should I do to fix it?

Upvotes: 1

Views: 167

Answers (4)

coderbhai
coderbhai

Reputation: 41

Let's say you have two string in which the first character is different. then you will not enter the loop as the condition of the loop (str1[i]==str2[i]) fails, therefore the condition should be ( str1[i]!='\0' && str2[i]!='\0' ). The '\0' is the last character of the c-style string. You can also use string built in functions like " strcmp(str1,str2) ".

Upvotes: 0

I have this code, but it does not work, why? Because your looping condition str1[i]==str2[i] will make the inner if condition be always false.

What should I do to fix it? Simple code:

for ( i=0; str1[i]==str2[i] && str1[i]!='\0'; i++) {
}
if ( str1[i]!=str2[i] ) {
    printf("They are not identical");
}

or

i=0;
while ( str1[i]==str2[i] && str1[i]!='\0' ) {
    i++;
}
if ( str1[i]!=str2[i] ) {
    printf("They are not identical");
}

Upvotes: 1

R Sahu
R Sahu

Reputation: 206717

Your for loop is:

for (i=0;str1[i]==str2[i];i++)
  if (str1[i]!=str2[i]){
     printf("They are not identical");
  }
  else continue;

Let's say str1 is "abc" and str2 is "xyz".

The conditional in the for loop will evaluate to false for i = 0. Hence, you will never get to the statement:

  if (str1[i]!=str2[i]){

Consequently, you will never execute:

     printf("They are not identical");

You can fix the logic error by using:

for (i=0; str1[i] != '\0' && str2[i] != '\0'; i++)
{
   if (str1[i]!=str2[i]) {
      break;
  }
}

// If at end of the loop, we have reached the ends 
// of both strings, then they are identical. If we
// haven't reached the end of at least one string,
// then they are not identical.
if ( str1[i] != '\0' || str2[i] != '\0' )
{
   printf("They are not identical");
}

Upvotes: 1

GProssliner
GProssliner

Reputation: 49

There is one important thing wrong here. First of all: The classic "c style string" is null terminated. Allthough there are other alternatives (like storing the length outside of the string), the null terminated string is part of the language (as string literals in code are null terminated by the compiler), and the runtime library (most string functions handle the \0 at the end of the string).

gets also appends a \0 at the end of the entered string: http://www.cplusplus.com/reference/cstdio/gets/

You are comparing not only the entered strings, but also anything (random) after that string in memory.

It should look like this:

for(int i=0;str1[i]==str2[i];i++){
 if(str1[i]==0) {
  printf("equal");
 }
}
printf("not equal");

There are other alternative, like using pointer. But on modern compilers they should produce roughly the same machine code.

Please note that there are C runtime library functions to compare strings:

strcmp is the most basic one, just two char *:

strncmp allows to specify the maximium chars to compare, do compare a part of a string:

There are other, just check out the links.

Please note that it's better to use the library functions, because even if in such a "simple" function. There are optimized ways to compare string. Like comparing in native word sizes. On 32 bit platforms you spend four time more time in comparation, not including the masking needed to perform byte wise operations.

Upvotes: 1

Related Questions