You are here:

C/switching person's name continues?!

Advertisement


Question
sorry for not being so clear but also i have to use pointers so that i have used this kind of code

lot of thanks
-------------------------

Followup To

Question -
well the thing is i ain't allowed to use malloc .. as u have explained it to me

the 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);
}

Answer -
Here is the corrected code:
#include <stdio.h>

main()
{
       char name1[25], name2[25];
       char temp[25];

       printf("please enter name1 => ");
       gets(name1);
       printf("please enter name2 => ");
       gets(name2);

       printf("Before swap:\n");
       printf("Name1 => %s\n", name1);
       printf("Name2 => %s\n", name2);

       strcpy(temp, name1);
       strcpy(name1,name2);
       strcpy(name2, temp);

       printf("After  swap:\n");
       printf("Name1 => %s\n", name1);
       printf("Name2 => %s\n", name2);
}

Answer
So, what is the complete problem statement?
You must use pointers and you should not use malloc!?
Is that right?

And are you allowed to use, any number of temporary variables?

If you make your problem statement clear, we can try to get the correct solution.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.