#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *ptr;
char temp[20];
if (strlen(argv[1]) < strlen(argv[2]))
{
strcpy(temp,argv[1]);
strcpy(argv[1],argv[2]);
strcpy(argv[2],temp);
}
ptr = strstr(argv[1],argv[2]);
if (ptr == NULL)
printf("Non-inclusive");
else
printf("%s is part of %s", argv[2], argv[1]);
return 0;
}
When I enter "abc abcd", I want to get "abc is part of abcd" as a result, but real result is "abc is part of abcdabc"
strcpy(argv[1],argv[2]);is not really valid. Think ofargvasconst char argv[]instead. Don't write to thechar[]s inargv.argvstrings is allowed, but writing past their end is of course not.ptr = strstr(argv[2], argv[1]);. Why do you have to swap the arguments? In case you tried to make the program check if the shorter string was part of the longer string regardless of their order, then this isn't it.