C program to remove all repeated characters from a given string

Category: C Program
Tags: #cprogram#string

Learn how to write a C program to remove all repeated characters from a given string. This guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.

In this article, we will discuss how to write a C program that removes all repeated characters from a given string, leaving only the first occurrence of each character. This task is useful in text processing applications where you need to clean up or standardize input data.

Objective

The goal is to create a C program that takes a string as input, removes all repeated characters, and then prints the modified string with only the first occurrences of each character.

Approach

To solve this problem, we will:

  1. Read the input string from the user.
  2. Use an auxiliary data structure to keep track of the characters that have already appeared in the string.
  3. Traverse the string and construct a new string that contains only the first occurrence of each character.
  4. Print the modified string.

Detailed Steps

  1. Read the input string:
    • Use standard input functions to get the string from the user.
  2. Track characters using an auxiliary data structure:
    • Use an array to keep track of characters that have already appeared in the string.
  3. Construct the modified string:
    • Traverse the input string, and for each character, check if it has already appeared using the auxiliary array. If not, add it to the new string.
  4. Print the modified string:
    • Print the resulting string after removing all repeated characters.

Write a C program to remove all repeated characters from a given string

Below is the complete C program that implements the above steps:

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int hash[256] = {0};  // Array to keep track of character occurrences
    int i, j = 0;

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Traverse the string and remove all repeated characters
    for (i = 0; i < strlen(str); i++) {
        if (hash[(int)str[i]] == 0) {
            hash[(int)str[i]] = 1;
            str[j++] = str[i];
        }
    }
    str[j] = '\0';  // Null terminate the modified string

    // Print the modified string
    printf("String after removing all repeated characters: %s", str);

    return 0;
}

Output

Enter a string: prrooccooooodding
String after removing all repeated characters: procding

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces and special characters are included.
    • It removes any trailing newline character added by fgets().
    • int hash[256] = {0} this is a shorthand way to initialize every element in the array to 0.
  2. Tracking Characters:
    • The program uses an array hash of size 256 to keep track of character occurrences. This array is initialized to zero.
  3. Constructing the Modified String:
    • The program uses a for loop to iterate over the input string. For each character, it checks if the character has already appeared by using the hash array.
    • If the character has not appeared (hash[(int)str[i]] == 0), it is added to the modified string, and the corresponding position in the hash array is set to 1.
    • The variable j is used to keep track of the position in the modified string.
  4. Output:
    • The modified string is printed, showing the result after all repeated characters have been removed.

Recommended Posts