This C program is designed to demonstrate basic string manipulation, input validation, and character replacement in C. It interacts with the user through the console to generate two strings and a replacement character, then filters the first string by replacing any characters that appear in the second string with the specified replacement character.
- Random Uppercase Letter Generation: Generates a string (
s1
) of 40 random uppercase letters. - User Input Validation: Prompts the user to enter a second string (
s2
) of uppercase letters, with length constraints. Also, prompts for a single replacement character. Validates all user inputs for correctness. - Character Filtering: Searches the first string (
s1
) for occurrences of any characters present in the second string (s2
) and replaces them with a user-specified character (ch
). - Program Restart Option: Allows the user to run the program multiple times without restarting the executable.
- Ensure you have a C compiler installed, such as GCC or Clang.
- Compile the program using your compiler. For example, with GCC, you would use the command
gcc -o charfilter charfilter.c
. - Run the compiled program from the command line. On Windows, you might type
charfilter.exe
, or on Linux/macOS,./charfilter
.
- Start the program: Follow the on-screen instructions. The program will first generate a random string of uppercase letters.
- Enter
s2
String: When prompted, enter a string of uppercase letters (A-Z) within the length limits specified by the program. - Enter Replacement Character: Enter a single character that will replace occurrences of
s2
characters ins1
. - Review Results: The program will display the original
s1
string, the user-entereds2
string and replacement character, and the filtereds1
string. - Repeat or Exit: Decide whether to run the program again or exit based on the prompt.
generateS1()
: Generates thes1
string with random uppercase letters.generateS2()
: Takes user input for thes2
string, ensuring it only contains uppercase letters and adheres to length restrictions.getReplacementCharacter()
: Prompts the user for a single character, used for replacement in the filtering process.strfilter()
: Replaces occurrences ofs2
characters ins1
with the replacement character.isUpperCaseLetter()
: Helper function to check if a character is an uppercase letter.duplicateArray()
: Duplicates thes1
array into another array for preservation before modification.
- The program disables security warnings in Microsoft Visual Studio with
_CRT_SECURE_NO_WARNINGS
. - Make sure to handle the input buffer carefully, especially when validating user inputs.
- The program uses the standard C library functions for I/O and string manipulation.