[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String Swapping -- C

Posted on 2007-10-10
2
Medium Priority
?
1,010 Views
Last Modified: 2008-01-09
How to swap two string ? Below code only works when two strings are same
How to make it to work for all other possibilites?


main()
{
char str1[] = "John";  // doesn't work for str1 = "ABCDEF" && str2 == "ABC"
char str2[] = "Kohan";

len = strlen(str1);
   
    xcopy(&str1,&str2,len);
   
    printf("\n Str1 :: %s",str1);
    printf("\n Str2 :: %s ", str2);
}

 xcopy(char *s1 , char *s2 ,int len)
  {
                while(len-- > 0)
      {
      *s1 ^= *s2;
      *s2 ^= *s1;
      *s1++ ^= *s2++;

      }

}

0
Comment
Question by:gauravflame
2 Comments
 
LVL 5

Accepted Solution

by:
abith earned 2000 total points
ID: 20047098
you have to pass maximum lenth as param of xcopy
linke this

int main()
{
char str1[] = "John";  // doesn't work for str1 = "ABCDEF" && str2 == "ABC"
char str2[] = "Kohan";

int len = (sizeof(str1) > sizeof(str2) ? sizeof(str1) : sizeof(str2)) ;
xcopy( str1, str2,len);
..


void  xcopy(char *s1 , char *s2 ,int len)
  {
      while(len-- > 0)
      {
        *s2 = *s1 + * s2;
        *s1 = *s2 - *s1;
        *s2 = *s2 - *s1;;
        *s1++ ;
        *s2++;
      }
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20047514
Why make it so complicated ? Why not just do :

        int main() {
            char *str1 = "John";
            char *str2 = "Kohan";

            swap_str(&str1, &str2);

            printf("\n Str1 :: %s", str1);
            printf("\n Str2 :: %s", str2);
            return 0;
        }

        void swap_str(char **str1, char **str2) {
            char *tmp = *str1;
            *str1 = *str2;
            *str2 = tmp;
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question