The "Remove Duplicates from Strings" program in C is designed to eliminate duplicate characters from a given string while preserving the order of their first occurrence. This program typically involves iterating through the string and checking if each character has already been added to a new string.

Implementation of Remove Duplicates from Strings Program

Here’s a simple implementation of this program in C:

#include <stdio.h> #include <string.h> int main() { char str[100], result[100]; // Arrays to hold the input string and the result int i, j, k = 0; // Loop counters // Ask user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read the input string // Remove newline character from the string if it exists str[strcspn(str, "\n")] = '\0'; // Iterate through each character in the input string for (i = 0; str[i] != '\0'; i++) { // Check if the character is already in the result int found = 0; for (j = 0; j < k; j++) { if (str[i] == result[j]) { found = 1; // Character is a duplicate break; } } // If the character is not found in the result, add it if (!found) { result[k++] = str[i]; // Add the character to the result } } result[k] = '\0'; // Null-terminate the result string // Display the result printf("String after removing duplicates: %s\n", result); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: For standard input and output functions.
    • #include <string.h>: For string manipulation functions like strcspn().
  2. Variable Declarations:

    • char str[100]: An array to hold the input string with a maximum length of 99 characters (the last character is reserved for the null terminator).
    • char result[100]: An array to store the resulting string without duplicates.
    • int i, j, k = 0: Loop counters. k is initialized to 0 to keep track of the length of the result.
  3. User Input:

    • The program prompts the user to enter a string, which is read using fgets(). This function allows spaces in the input.
  4. Remove Newline Character:

    • The program removes any newline character that fgets() might include by using strcspn().
  5. Character Iteration:

    • A for loop iterates through each character of the input string.
  6. Duplicate Check:

    • Inside the loop, another for loop checks if the current character (str[i]) already exists in the result string.
    • If the character is found, found is set to 1, indicating that it is a duplicate.
  7. Add Unique Characters:

    • If the character is not found (i.e., found remains 0), it is added to the result string at index k, and k is incremented.
  8. Null-Termination:

    • After processing all characters, the result string is null-terminated to make it a valid string.
  9. Display Results:

    • Finally, the program prints the modified string with duplicates removed.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc remove_duplicates.c -o remove_duplicates
  2. Execute the Program:

    ./remove_duplicates
  3. Input Data: Enter a string when prompted.

Example Input/Output

Input:

Enter a string: Hello World!

Output:

String after removing duplicates: Helo Wrd!

Conclusion

The "Remove Duplicates from Strings" program in C demonstrates string manipulation and basic data handling techniques. It reinforces the understanding of loops, conditionals, and arrays. This type of program can be modified to include additional functionality, such as removing duplicates while ignoring case sensitivity or counting the number of duplicates removed.