-3

I am writing a simple code to find a string s1 from a string s2 and then saving it in string s3. The function returns 1 if the string s1 is present in s2 else 0. I am using pointers as parameters. The problem is that even when I am assigning s1 to n, line 30 of code isn't providing me correct output. Also, the code is behaving abnormally. Can you please point out the error?

#include<string.h>
#include<stdio.h>
int code(char * s2, char * s1, char * s3)
{
    char h[] = "";
    strcpy(h, s2);
    printf("%s\n", h);

    char n[] = "";
    strcpy(n, s1);
    printf("%s\n", n);

    char b[] = "";

    int i = 0, j = 0, flag = 0, flag2 = 0;

    for (i = 0; n[i] != '\0'; i++)
    {
        printf("%d\n", i);
        printf("%c\n", n[i]);
        for (j = 0; h[j] != '\0'; j++)
        {
            printf("%c\n", h[j]);
            if (n[i] == h[j])
            {
                flag = 1;
                break;
            }

        }
        if (flag == 1)
        {
            break;
        }
    }

    printf("%d\n", i);
    printf("%d\n", j);

    printf("%c\n", n[i]);
    printf("%c\n", h[j]);

    printf("\n");

    if (flag == 1)
    {
        printf("Here:1\n");
        printf("%c\n", h[j]);
        while (n[i] != '\0')
        {
            printf("Here: %d\n", i);
            printf("%c\n", n[i]);
            printf("%c\n", h[j]);
            if (n[i] == h[j])
            {
                printf("Here:2 %d %d\n", i, j);
                printf("%c\n", n[i]);
                printf("%c\n", h[j]);
                b[i + 1] = n[i];
                i = i + 1;
                j = j + 1;

            }
            else
            {
                printf("Here:3\n");
                flag2 = 1;
                break;
            }
        }

        if (flag2 == 1)
        {
            printf("Here:4\n");
            return 0;
        }
        else
        {
            printf("Here:5\n");
            b[i + 1] = '\0';
            s3 = b;
            return 1;
        }

    }
    else
    {
        printf("Here:6\n");
        return 0;
    }

}

Input: s2: vanilla s1: lla

Output: 1

Kami Kaze
  • 2,069
  • 15
  • 27
Coder1
  • 11
  • 5
  • `strcpy` does _not_ allocate memory for you. You should do: `char *h=malloc(strlen(haystack)+1); strcpy(h, haystack);` – Paul Ogilvie Sep 27 '18 at 08:38
  • Also your code looks terribly convoluted. And you never use `s3` etc. There are many issues. You should probably start with something simpler and read the chapters dealing with strings and with pointers in your C text book. – Jabberwocky Sep 27 '18 at 08:45
  • A `char*` is not some pre-made string class like in other languages. It's just a low level address pointing at memory allocated elsewhere. See https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ – Lundin Sep 27 '18 at 08:45
  • @KrishanPalSingh OK, better now, but please show also how you call your `code` function. Provide a [MCVE]. – Jabberwocky Sep 27 '18 at 08:57
  • 1
    `char h[] = ""; strcpy(h, s2);` --> `h[]` is only a 1 `char` array. The `stcpy()` will certainly overfill the destination. – chux - Reinstate Monica Sep 27 '18 at 09:11

1 Answers1

0

The char arrays h, n and b hava a size of 1.

Do following modifications in your code:

  char h[100] = "";  // << change here
  strcpy(h, s2);
  printf("%s\n", h);

  char n[100] = "";  // << change here
  strcpy(n, s1);
  printf("%s\n", n);

  char b[100] = "";  // << change here

With these modifications the arrays can contain up to 100 characters. Now your code seems to work more or less.

Disclaimers:

  • even with these modifications your code is still terrible and inefficient
  • the modifications are just quick and dirty hacks, the function won't work if the strings involved are longer than 99
  • there may be other problems in your code
  • s3 = b; this won't work for several reasons, but that's another topic
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115