Introduction:

This C program is designed to remove duplicate characters from a string provided by the user. The program scans the string and keeps only the first occurrence of each character, effectively filtering out any repetitions. This functionality can be useful in various applications, such as text processing, data cleaning, and formatting.

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

void removeDuplicates(char *str) {
    int length = strlen(str);
    char result[100];
    int index = 0;

    for (int i = 0; i < length; i++) {
        int j;
        for (j = 0; j < index; j++) {
            if (str[i] == result[j]) {
                break;
            }
        }
        if (j == index) {
            result[index++] = str[i];
        }
    }
    result[index] = '\0';
    strcpy(str, result);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = 0; // Remove newline character

    removeDuplicates(str);
    printf("String after removing duplicates: %s\n", str);
    return 0;
}

Include Header Files:

  • #include <stdio.h>: This header file is necessary for standard input and output functions like printf and fgets.
  • #include <string.h>: This header file is included for string manipulation functions such as strlen and strcpy.

Function to Remove Duplicates:

  • void removeDuplicates(char *str): This function accepts a string as a parameter.

Declare Variables:

  • int length = strlen(str);: Calculates the length of the input string.
  • char result[100];: An array to hold the result string without duplicates. Ensure its size is sufficient.
  • int index = 0;: An index variable to track the position in the result array.

Loop Through the Input String:

  • for (int i = 0; i < length; i++): Iterates through each character of the original string.

Check for Duplicates:

  • int found = 0;: Initializes a flag variable to track if the current character is already in the result.
  • The inner loop (for (int j = 0; j < index; j++)) checks if the current character (str[i]) already exists in the result array.
    • If it finds a match, it sets found = 1 and breaks out of the inner loop.

Add Unique Characters to the Result:

  • If the character is not found in result (i.e., found remains 0), it adds the character to the result array and increments the index (index++).

Null-Terminate the Result String:

  • result[index] = '\0';: After processing all characters, this line adds a null terminator to the end of the result string.

Copy Result Back to Original String:

  • strcpy(str, result);: Copies the modified string (without duplicates) back to the original string variable.

Main Function:

  • char str[100];: Declares an array to hold the user-input string.
  • printf("Enter a string: ");: Prompts the user to enter a string.
  • fgets(str, sizeof(str), stdin);: Reads a line of text from standard input, allowing for spaces.
  • str[strcspn(str, "\n")] = 0;: Removes the newline character that fgets adds.
  • removeDuplicates(str);: Calls the function to remove duplicates from the string.
  • printf("String after removing duplicates: %s\n", str);: Outputs the string without duplicates.

Return Statement:

  • return 0;: Indicates that the program executed successfully.

Input/Output Block:

Input:

  • The user is prompted to enter a string, which may contain letters, digits, and special characters.

Example Input:

Enter a string: programming

Output:

  • The program outputs the string with duplicate characters removed.

Example Output:

String after removing duplicates: progamin

How to Run:

  1. Copy the code into a C compiler or IDE.
  2. Compile the program.
  3. Run the executable and input a string when prompted.
  4. View the output string with duplicates removed.

Conclusion

The duplicate removal program successfully filters out repeated characters from a string, preserving the order of their first occurrences. This is particularly useful for applications requiring unique character sequences. By leveraging an array to track character occurrences, the program efficiently processes the input, showcasing effective use of data structures and string manipulation in C. This approach can be applied in various contexts, including data cleaning and text analysis.

Leave a Reply

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

Verified by MonsterInsights