2

I am not able to type the second input in the command prompt, it says my program has stopped. I am just trying my own code. I have added two functions to check for the length and contents of the string. And i can simply use strcmp(), but i am avoiding to use that for practice.

#include<stdio.h>
#include<string.h>
int lencompare(char *str1, char *str2){
    int len1, len2;
    len1 = strlen(str1);
    len2 = strlen(str2);
    return (len1==len2) ? 1 : 0;
}
int compare(char *str1, char *str2){
    int i =0, flag=0;
    if(lencompare(str1, str2)==1){
        while(*(str1+i) != '\0');
        {
            if(*(str1+i) == *(str2+i))
             flag = 1;
            else
             flag = 0;
            i++;
        }
        return flag;
    }
    if(lencompare(str1, str2) == 0){
        printf("Length is Different");
    }
}
int main(){
    char *str1 , *str2 ;
    fgets(str1, 20, stdin);
    fgets(str2, 20, stdin);
    if(compare(str1, str2) == 1){
        printf("both length and content are same");
    }
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sridhar
  • 21
  • 3
  • So what output does your program show you? A Screenshot would be lovely. – J. Murray Oct 08 '19 at 14:47
  • 7
    str1 and str2 need to be buffers, up to 20 chars - you haven't allocated the memory for them. try char str1[ 21 ], str2[ 21 ]; – Neil Oct 08 '19 at 14:48
  • 1
    You should revise your use of `flag`. You keep setting and clearing it. Initialise it to `1` and set it to `0` if any characters are different. If they are the same, don't touch it. Also in `int compare()` not all control paths return a value (see compiler warnings). – Weather Vane Oct 08 '19 at 14:52
  • [How do strings and char arrays work in C?](//stackoverflow.com/q/12718929) – 001 Oct 08 '19 at 14:53
  • 2
    `str1[i]` is the same thing and a lot easier to understand than `*(str1+i)`. – Andrew Henle Oct 08 '19 at 15:14

1 Answers1

6

For starters these variables

char *str1 , *str2 ;

are not initialized. So the program already has undefined behavior. You need to declare character arrays like for example

enum { N = 20 };
char str1[N], str2[N];

The standard function fgets can append the new line character '\n' to the entered string provided that there is enough space in the character array.

You should remove this character. For example

fgets(str1, 20, stdin);
fgets(str2, 20, stdin);

str1[ strcspn( str1, "\n" ) ] = '\0';
str2[ strcspn( str2, "\n" ) ] = '\0';

The function lencompare should be declared and defined like

int lencompare( const char *str1, const char *str2 )
{
    return strlen( str1 ) == strlen( str2 );
}

The function compare has undefined behavior because in some paths of execution it returns nothing.

Also it should be declared like

int compare( const char *str1, const char *str2 );

Within the function you two times call the function lencompare:

if(lencompare(str1, str2)==1){

and

if(lencompare(str1, str2) == 0){

The program can look the following way

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

int lencompare( const char *str1, const char *str2 )
{
    return strlen( str1 ) == strlen( str2 );
}

int compare( const char *str1, const char *str2 )
{
    int equal = lencompare( str1, str2 );

    if ( equal )
    {
        while ( *str1 == *str2 && *str1 ) 
        {
            ++str1;
            ++str2;
        }           

        equal = *str1 == *str2;
    }

    return equal;
}   

int main(void) 
{
    enum { N = 20 };
    char str1[N], str2[N];

    fgets( str1, sizeof( str1 ), stdin );
    fgets( str2, sizeof( str2 ), stdin );

    str1[ strcspn( str1, "\n" ) ] = '\0';
    str2[ strcspn( str2, "\n" ) ] = '\0';

    if ( compare( str1, str2 ) )
    {
        puts( "both length and content are same" );
    }

    return 0;
}

Its output might look like

Sridhar
Sridhar
both length and content are same

Pay attention to that instead of the while loop in the function compare you could use the standard C function strcmp. In this case the function compare will be very simple

int compare( const char *str1, const char *str2 )
{
    return strcmp( str1, str2 ) == 0;
}   

In fact there is no need to compare preliminary the lengths of the strings.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335