Confused Strings!

So I have a program that is supposed to allow you to kill any window by using the "kill" command in Cygwin (the environment that I use). Yes, it is C, not C++. What happens for some reason is I search for something like notepad, and it thinks all of a sudden that my cmd string says epad instead of bin/kill -f. Please help!

Program:
#include <stdio.h>
#include <windows.h>
#include <string.h>

int main()
{
char keyword[100], pid[100];
char cmd[] = "bin/kill -f ";
char search[] = "ps -W | grep ";
printf("Enter a keyword to search for pids of windows: ");
scanf("%s", &keyword);
strcat(search, keyword);
system(search);
printf("Copy a pid from above corresponding to the desired window to close and hit enter: ");
scanf("%s", &pid);
strcat(cmd, pid);
Sleep(3000);
printf("Window \'%s\' has been closed.\n", pid);
printf("Command: %s", cmd);
system(cmd);
return 0;
}

Output:
$ ./ultkill
Enter a keyword to search for pids of windows: notepad
1500 0 0 1500 ? 0 08:08:31 C:\Windows\System32\notepad.exe
Copy a pid from above corresponding to the desired window to close and hit enter: 1500
Window '1500' has been closed.
sh: epad1500: command not found
Command: epad1500

Last edited on
you cannot do this: strcat(cmd, pid);
because cmd doesn't provide enough memory (it's exact the size of "bin/kill -f ").

try something like char cmd[1000] = "bin/kill -f ";
Thanks! That appeared to work.
Topic archived. No new replies allowed.