C Online Compiler
Example: Toggle Characters using ASCII Values in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Toggle Characters using ASCII Values #include <stdio.h> // For printf, gets #include <string.h> // For strlen (optional, can use null terminator directly) #define MAX_LENGTH 100 int main() { char str[MAX_LENGTH]; // Step 1: Prompt user for input string printf("Enter a string: "); fgets(str, MAX_LENGTH, stdin); // Read string including spaces // Remove trailing newline character if present str[strcspn(str, "\n")] = 0; // Step 2: Iterate through the string to toggle characters int i = 0; while (str[i] != '\0') { // Check if the character is an uppercase letter if (str[i] >= 'A' && str[i] <= 'Z') { str[i] = str[i] + 32; // Convert to lowercase } // Check if the character is a lowercase letter else if (str[i] >= 'a' && str[i] <= 'z') { str[i] = str[i] - 32; // Convert to uppercase } // If not an alphabet, leave it unchanged i++; } // Step 3: Print the modified string printf("Toggled string: %s\n", str); return 0; }
Output
Clear
ADVERTISEMENTS