In this article, we will write a C program to swap first occurrence of a character in a String with the given character.
C Program to swap first occurrence of character with another character
A brief explanation of the statements is provided in the program itself using comments. You can find the detailed explanation at the end of this article.
#include <stdio.h>
#include <string.h>
// It swaps the first occurrence of char1 with char2 in the string str
void swap_first_occurrence(char *str, char char1, char char2) {
// Get the length of the string
int len = strlen(str);
// Iterate though the string str to find the first occurrence of char1
for (int i = 0; i < len; i++) {
if (str[i] == char1) {
// Replace char1 with char2
str[i] = char2;
break; // Exit the loop after the first occurrence is replaced
}
}
}
int main() {
// It stores the string entered by user
char str[100];
//char1: character that needs to be replaced.
//char2: character that will replace char1.
char char1, char2;
// Read the input string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character at the end of the string if present
str[strcspn(str, "\n")] = '\0';
// Get the character from user that needs to be swapped out
printf("Enter the character to be swapped out: ");
scanf(" %c", &char1);
// Get the character from user that needs to be swapped in
printf("Enter the character to be swapped in: ");
scanf(" %c", &char2);
// Call the function to swap the first occurrence of char1 with char2
swap_first_occurrence(str, char1, char2);
// Print the modified string
printf("Modified string: %s\n", str);
return 0;
}
Output:
Enter a string: hello world
Enter the character to be swapped out: o
Enter the character to be swapped in: a
Modified string: hella world
Explanation of the program
- Header files:
#include <stdio.h>
: Includes the standard input/output library forprintf
andscanf
functions.#include <string.h>
: Includes the string handling library.
- swap_first_occurrence() Function:
int len = strlen(str);
It calculates the length of the input string.- It runs a
for
loop to iterate through the string to find the first occurrence ofchar1
:if (str[i] == char1)
: Checks if the current character ischar1
.- If
char1
is found, replace it withchar2
. - User break statement to terminate the loop after replace is done.
- main Function:
char str[100];
: This char array is used to hold the input string.char char1, char2;
: Both of these variables are entered by user, the first occurrence ofchar1
is need to be replaced withchar2
.fgets(str, sizeof(str), stdin);
: Reads the input string from the user.str[strcspn(str, "\n")] = '\0';
: Removes the newline character from the input string if it is present.scanf(" %c", &char1);
andscanf(" %c", &char2);
: Get the characters value from the user. The space before%c
ensures that any leading whitespace characters are ignored.swap_first_occurrence(str, char1, char2);
: Calls the function to swap the first occurrence ofchar1
withchar2
.printf("Modified string: %s\n", str);
: Prints the modified string.
Leave a Reply