Click to See Complete Forum and Search --> : char problem in C


Seko
02-26-2004, 05:32 AM
i have a char array: char x[20];
i did the following: strcpy(x, argv[2]);

but i want to add one space in the begining and end of the char array for example say argv[2] is "abc" so i want x to contain " abc " how can i do this?

thx

Minime80
02-26-2004, 05:57 AM
this should pretty much do it
char x[20];
strcpy(x, " ");
strcat(x, argv[2]);
strcat(x, " ");

Seko
02-26-2004, 06:16 AM
Thx very much i could not think of it :) anyway i am new to C programming....

Minime80
02-26-2004, 07:40 AM
Anytime. We're here to help.

CyberCoder
02-26-2004, 12:24 PM
another way to do that:
int x[20];
sprintf(x," %s ",argv[2]);

dchidelf
02-26-2004, 01:55 PM
or

char x[20];
sprintf(x," %.17s ",argv[2]);


That should ensure the string will actually fit within the allocated space.

Seko
02-26-2004, 02:48 PM
Wow prety much C coders here...i want to be a good C coder too thx for different technics..By the way another question i have in the forum about exec()....

take a look:)

bwkaz
02-26-2004, 08:16 PM
Or

char x[20];

snprintf(x, 20, " %s ", argv[2]); (another way to not overflow the buffer).