C/to be continues!
Expert: Narendra - 7/8/2006
Question"sorry for making it so unlclear to you. i'm allowed to use temp
variables as well as pointers but i'm not allowed to use Malloc. and the main
things is i have 2 kind of output like if i have a name jack,well
output= well jack
and if the name was jack well the output=well,jack
i hope i'm clear now to you
thanks a lot in advance
the other thing is i have this code but i don't know where's the
problem thanks
main()
{
char name[25];
char temp[25];
char *ptr;
int temp1;
char b;
printf("please enter name ");
gets(name);
ptr=strlen(name);
*ptr='\0';
*ptr++;
strcpy(temp,ptr);
strcat(temp,",");
strcat(temp,name);
puts(name);
}
thanks a lot
AnswerOK. Till now I was thinking that you have two arrays having two different names and you are trying to swap.
After looking at the code, now I know that you have only one array and you want to swap the things inside this.
So, here is the modified code.
Hope this works for you:
#include <stdio.h>
main()
{
char name[24];
char name1[12], name2[12];
char temp[25];
int len;
char *ptr1 = name1, *ptr2 = name2;
printf("please enter name1 ");
gets(name);
sscanf(name, "%s %s", name1, name2);
printf("Before swap => %s\n", name);
len = strlen(name1);
if (name1[len - 1] == ',') name1[len - 1] = '\0';
else
{
len = strlen(name2);
name2[len] = ',';
name2[len + 1] = '\0';
}
strcpy(temp, ptr1);
strcpy(name1, name2);
strcpy(name2, temp);
sprintf(name, "%s %s", name1, name2);
printf("After swap => %s\n", name);
}