
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to swap two strings
For swapping two strings from one location to another location, we use strcpy() function.
An array of characters (or) collection of characters is called a string.
Declaration
Following is the declaration for an array −
char stringname [size];
For example, char string[50]; string of length 50 characters.
Initialization
- Using single character constant
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
- Using string constants
char string[10] = "Hello":;
Accessing
There is a control string "%s" used for accessing the string till it encounters ‘\0’
strcpy ( )
This function is used for copying source string into destination string.
The length of the destination string is greater than or equal to the source string.
The syntax for strcpy() function is as follows −
strcpy (Destination string, Source String);
For example,
char a[50]; char a[50]; strcpy ("Hello",a); strcpy ( a,"hello"); output: error output: a= "Hello"
Program
Following is the C program to swap two strings by using strcpy() function −
#include<stdio.h> #include<string.h> main(){ char s1[10],s2[10],s3[10]; printf("Enter String 1
"); gets(s1); printf("Enter String 2
"); gets(s2); printf("Before Swapping
"); printf("String 1 : %s
",s1); printf("String 2 : %s
",s2); strcpy(s3,s1); strcpy(s1,s2); strcpy(s2,s3); printf("After Swapping:
"); printf("String 1 : %s
",s1); printf("String 2 : %s
",s2); }
Output
When the above program is executed, it produces the following result −
Enter String 1 Tutorial Enter String 2 Point Before Swapping String 1: Tutorial String 2: Point After Swapping: String 1: Point String 2: Tutorial
- Related Articles
- C++ Program to Swap Two Numbers
- C function to Swap strings
- Swap two Strings without using temp variable in C#
- C++ program to get length of strings, perform concatenation and swap characters
- C++ Program to Concatenate Two Strings
- Java program to swap two integers
- Java Program to Swap Two Numbers.
- Haskell Program to Swap Two Numbers
- Kotlin Program to Swap Two Numbers
- C++ Program to Compare two strings lexicographically
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- Program to add two binary strings in C++
- 8085 program to swap two 8-bit numbers
- How to Swap Two Numbers in Swift Program?
- Swap two Strings without using third user defined variable in Java

Advertisements