BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to swap first occurrence of a character in a String

Last Updated: May 17, 2024 by Chaitanya Singh | Filed Under: C Programs

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

  1. Header files:
    • #include <stdio.h>: Includes the standard input/output library for printf and scanf functions.
    • #include <string.h>: Includes the string handling library.
  2. 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 of char1:
      • if (str[i] == char1): Checks if the current character is char1.
      • If char1 is found, replace it with char2.
      • User break statement to terminate the loop after replace is done.
  3. 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 of char1 is need to be replaced with char2.
    • 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); and scanf(" %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 of char1 with char2.
    • printf("Modified string: %s\n", str);: Prints the modified string.

Top Related Articles:

  1. C Program to swap first and last elements of an array
  2. C Program to find prime numbers in a given range
  3. C Program to read and print employee details using structure
  4. C Program to Access Array Elements Using Pointer
  5. C Program to swap first and last digit of a number

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap