Swap Two Strings in C Language

program-to-swap-two-strings-in-c-language

Program Description:

Write a Program to swap two strings in c programming language. The program should accept two strings from the user and swap the contents of the two strings.

📢 This Program is part of the C String Practice Programs series

We are going to look at a couple of methods to swap two strings in C and Each program will be followed by a detailed step-by-step explanation of the program and program output. We also provided instructions for compiling and running the program using your compiler.

Let’s look at the example input and output of the program.

Excepted Input and Output:

Program Input:

Enter the first string : Git
Enter the second string: SVN

Program Output:

Strings before Swapping: str1:'Git' and str2:'SVN'

Strings After Swapping: str1:'SVN' and str2:'Git'

The input strings content are swapped.

Prerequisites:

It is recommended to know the basics of the Strings and Functions in C language. Please go through the following articles if you want to refresh your concepts.

Swap Two Strings in C using string library functions:

In this method, We are going to use the strcpy library function to copy the strings. Let’s look at the step by step explanation of the program.

Program Explanation: Swap Two Strings in C:

  1. Start the program by creating a Macro named SIZE with the value 100. The SIZE macro holds the max size of the string.
  2. Declare two strings. Let’s say these strings as str1 and str2. The size of the strings is equal to the SIZE macro. We use these strings to store the input strings. – char str1[SIZE], str2[SIZE];
  3. Initialize another string called temp with the same size as the above strings. This temp string is used to swap the above two strings. char temp[SIZE] = {0};
  4. Take the two strings from the user and update the str1 and str2 variables. We are using the gets() function to read the strings. the gets function doesn’t check the boundaries. so be careful while using the gets function.
  5. To Swap Two Strings in C, We are going to use a temporary string. Here is the process to swap to strings.
  6. First of all, copy the str1 string to the temp string using strcpy function. This is the backup of str1 string.
  7. Then copy the contents of the str2 string to the str1 string. At this point, the characters of str1 will be overwritten by the str2 string. So Now the str1 is holding the str2.
  8. Finally, Copy the temp string to the str2 string. Now, str2 contains the initial str1 string.
  9. Once the above steps 6, 7, and 8 are completed, The strings str1 and str2 are swapped successfully.
  10. Print the swapped strings on the console using printf function.

Program to Swap two strings using strcpy function:

Let’s convert the above algorithm to the c program

Program Output:

Let’s compile and run the program. We are using the GCC compiler on Ubuntu Linux.

swap-two-strings-in-c-using-library-function-program-output

As we can see from the above output, The input strings Investor and Trader has been swapped successfully.

Let’s look at another example.

The input strings Lewis Hamilton and George Russell have been swapped and str1 contains George Russell and str2 contains Lewis Hamilton.

Program to Swap Two String in C without using library Functions:

We are not going to use library functions to swap the strings in this method. So we need to implement our own string copy function. We have already discussed the custom string copy function in our earlier posts.

Swap Two strings without using library function program explanation:

Create a function called mystrcpy to copy a string to another string. Here is the prototype of the function.

char * mystrcpy(char *dest, char *src)

The mystrcpy() function takes two formal arguments. The destination string(dest) and source string(src). This function copies the src string to the dest string.

It also returns the copied string back to the caller.

This function Iterates over the Original String using a For Loop and Copy elements from source string to destination string.

Here is program to Swap two strings using custom user defined strcpy function (i.e mystrcpy)

Finally, We replaced the strcpy() function calls in the main() function with the mystrcpy() function.

Program Output:

Compile the program.

gcc swap-two-strings-func.c

Run the program.

swap-two-strings-in-c-using-library-function-program-output

As we can see from the above output, The input string USA and UK are swapped.

The program is properly SWAPPING the given strings.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] an earlier article, We looked at how to Swap two strings in C. In today’s article, We will look at the anagram program in c language. The program should accept […]

Leave a Reply