-1

I am trying to find the length of string 1(s1).. but the value that it is giving is 0, and input for s1 = "HELLO", because of this error not able to execute the for loop where I use length of string 1.

Following is the code...works fine in gcc version 4.9.1 (Ubuntu 4.9.1-16ubuntu6) but not in online compiler( gcc 4.9.2, C99 standard )

/*To check if common characters are present in two strings*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio_ext.h>
#define SIZE 100
int main() {
int m, i, j, t, len1;
char *s1, *s2;
scanf("%d", &t);  //No. test cases
__fpurge(stdin);

for(m = 0; m < t; m++)
{
    int res = 0;

    s1 = (char *)malloc( SIZE * sizeof( char ));
    s2 = (char *)malloc( SIZE * sizeof( char ));

    fgets(s1, SIZE, stdin);
    fgets(s2, SIZE, stdin);

    *(s1 + strlen(s1) - 1) = '\0';
    *(s2 + strlen(s2) - 1) = '\0';

    len1 = strlen(s1); // len1 is storing as 0
    printf("%d", len1 ); 
    for (i = 0; i < strlen(s1); i++)  
    {
        for (j = 0;j < strlen(s2); j++)
        {
            if ( *(s1 + i) == *(s2 + j) )
            res = 1;
        }
    }

if(res == 1)
printf( "YES\n" );
else
printf("NO\n");
}
return 0; 
}
Rhys Jones
  • 5,348
  • 1
  • 23
  • 44
Liju Thomas
  • 1,054
  • 5
  • 18
  • 25
  • 1
    What is a dump of s1 right after defining it giving? – Matheno Mar 27 '15 at 16:43
  • What is `SIZE` defined as? I hope not `5`!! `5` is not enough to hold `"HELLO"` ... you need to account for the `'\0'` terminator *and also the line breaks*. – pmg Mar 27 '15 at 16:44
  • 1
    I used `10` as `SIZE` and your code is working for me... – Matt Ko Mar 27 '15 at 16:45
  • I used `#define SIZE "test_FILE" __FILE__` and all I got was a load of errors. I must be doing something wrong then. – Jongware Mar 27 '15 at 16:48
  • 2
    @Jongware Why do you want to define such strange stuff? Use a numerical value like `10` to define a count of bytes or characters. – harper Mar 27 '15 at 16:49
  • I used #define SIZE 50.. but the problem is while it executes strlen(s1).. its output is 0 – Liju Thomas Mar 27 '15 at 16:53
  • But we have to guess to what `SIZE` is, as it seems quite important to know! – Jongware Mar 27 '15 at 16:53
  • Are you sure input is actually `"HELLO"`? your code works fine, maybe you should post your whole code (except if it is really long) – axelduch Mar 27 '15 at 17:02
  • when I execute the same code in online gcc compiler (gcc 4.9.2, C99 standard), I am not getting the expected output... But when I do the same in my ubuntu gcc compiler (gcc version 4.9.1 (Ubuntu 4.9.1-16ubuntu6) ), it gives correct output. – Liju Thomas Mar 27 '15 at 17:05
  • 2
    [Every time someone casts the result of malloc in C a robin dies.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Paul R Mar 27 '15 at 17:09
  • The most efficient way to remove the trailing new-line: http://stackoverflow.com/a/28462221/694576 – alk Mar 27 '15 at 17:19

1 Answers1

3

There is a fairly standard way to handle stripping the newline from input following a read from a file with either fgets or getline. There are variations, but all replace the newline with a null-terminating character. Here is an example using your code:

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

#define SIZE 64

int main (void) {

    char *s1 = NULL;
    char *s2 = NULL;
    size_t len1 = 0;
    size_t len2 = 0;

    s1 = malloc (SIZE * sizeof *s1);
    s2 = malloc (SIZE * sizeof *s2);

    printf ("\n Enter s1: ");
    fgets (s1, SIZE, stdin);
    printf ("\n Enter s2: ");
    fgets (s2, SIZE, stdin);

    /* strip newline or carriage rtn    */
    len1 = strlen (s1);
    while (len1 > 0 && (s1[len1-1] == '\n' || s1[len1-1] == '\r'))
        s1[--len1] = 0;

    len2 = strlen (s2);
    while (len2 > 0 && (s2[len2-1] == '\n' || s2[len2-1] == '\r'))
        s2[--len2] = 0;

    printf ("\n  len1 : %zu   s1 : %s\n  len2 : %zu   s2 : %s\n\n", len1, s1, len2, s2);

    if (s1) free (s1);
    if (s2) free (s2);

    return 0;
}

Output

$ ./bin/fgets_strip

 Enter s1: This is s1

 Enter s2: This is s2

  len1 : 10   s1 : This is s1
  len2 : 10   s2 : This is s2
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 2
    I assume you added `\r` to be Windows-compatible ... but is that necessary? [A good reference](http://en.cppreference.com/w/cpp/io/c/fgets) will say it stores "a newline character", and surely that is just only the `\n`? – Jongware Mar 27 '15 at 17:13
  • code is working fine.. learnt new method to strip newline or carriage return .. thanks! @David C Rankin – Liju Thomas Mar 27 '15 at 17:27
  • 1
    Glad I could help... and yes the carriage return is for the dark side, or any files copied from flash-drives, etc. that still have `CRLF` endings. – David C. Rankin Mar 27 '15 at 18:10
  • @LijuThomas: if this answered your question satisfactorily, click the check mark. As a new SO member, you may want ro read the introductory [tour]. – Jongware Mar 28 '15 at 04:50