-2

I need to know whats wrong with this code.

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

int main() {
    char a[50], b[100], c[5000];
    char *ret;
    //enter first name
    gets(a);
    //enter secend name 
    gets(b);
    //enter statement
    gets(c);

    strcat(a,b);

    if(strstr(c,a) != NULL) {
        printf("found your full name");
    } else {
        printf("not found your full name");
    }
    return 0;
}

It does not work when, I use the following lines:

mohamed
ramadan
abdelrhmanamirelbatanonywoofymohamedramadanahmedalyomarelazazyahmedkamelahmedsa‌​lemessamelnaggarkhaledhelmy

It should find something, but the programm tells me, that it hasn't.

hellow
  • 12,430
  • 7
  • 56
  • 79
  • 1
    @MohamedHesham If your teacher/book told you to use `gets`, throw them out of a window. – user4520 May 23 '15 at 17:12
  • [program tell me work "found your full name"](http://ideone.com/OkN5VR) – BLUEPIXY May 23 '15 at 17:31
  • 1
    @MohamedHesham that program works, although it's not very good code. What is your question? Did you put those "quotes" round your inputs? If so, it *won't* work. – Weather Vane May 23 '15 at 17:41
  • 1
    @CarlProthman this it a problem which i try to solve it ..Input Format: 3 First line is your first name Second line is your last name Third line contain the Geeks List Output Format: If you found your name in the list print "Invited" otherwise print "I don't have time for parties, I have thigs to do." Sample Input: line1: mohamed ; line2: ramadan; line3 :abdelrhmanamirelbatanonywoofymohamedramadanahmedalyomarelazazyahmedkamelahmedsalemessamelnaggarkhaledhelmy; Sample Output: Invited – Mohamed Hesham May 23 '15 at 21:09
  • @szczurcio i will try to replace them if problem with them not with me :) – Mohamed Hesham May 23 '15 at 21:11
  • format your question well enough for all to understand easily buddy.. there are options to distinguish code snippets from other parts and the like. – Harshith Rai Sep 07 '18 at 09:25

1 Answers1

2
  • Point 1

    As per the man page of strcat()

    char *strcat(char *dest, const char *src);

    If dest is not large enough, program behavior is unpredictable

    In your case,

    strcat(a,b);
    

    a maybe not having enough memory to hold the concatenated string. Possible UB. Change the logic.

  • Point 2

    gets() suffers from buffer overflow issue. Use fgets() instead.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261