Alphabetic Substitution Cipher
Implementing a Caesar-like cipher that shifts English letters by one position while inverting their case. Lowercase letters become uppercase and shift forward, while uppercase letters become lowercase and shift forward.
#include <stdio.h>
#include <ctype.h>
int main() {
int current_char;
while ((current_char = getchar()) != '\n') {
if (current_char >= 'a' && current_char <= 'y') {
putchar(toupper(current_char) + 1);
} else if (current_char == 'z') {
putchar('A');
} else if (current_char >= 'A' && current_char <= 'Y') {
putchar(tolower(current_char) + 1);
} else if (current_char == 'Z') {
putchar('a');
} else {
putchar(current_char);
}
}
return 0;
}
Basic Arithmetic Calculator
Processing a sequence of arithmetic operations until an equals sign is encountered. Division by zero or invalid operators trigger an error state.
#include <stdio.h>
int main() {
int result, operand;
char operator;
int error = 0;
scanf("%d", &result);
while (scanf("%c", &operator) == 1 && operator != '=') {
scanf("%d", &operand);
if (operator == '+') {
result += operand;
} else if (operator == '-') {
result -= operand;
} else if (operator == '*') {
result *= operand;
} else if (operator == '/') {
if (operand == 0) {
error = 1;
} else {
result /= operand;
}
} else {
error = 1;
}
if (error) break;
}
if (error) {
printf("ERROR");
} else {
printf("%d", result);
}
return 0;
}
Capitalize Initial Letters
Converting the first character of each word in a string to uppercase based on preceding space characters.
#include <stdio.h>
#include <ctype.h>
int main() {
int c;
int is_word_start = 1;
while ((c = getchar()) != '\n') {
if (is_word_start && c >= 'a' && c <= 'z') {
c = toupper(c);
}
putchar(c);
is_word_start = (c == ' ');
}
return 0;
}
Locating Maximum Value and Index
Finding the highest value within an integer array and returning the index of its first occurrence.
#include <stdio.h>
int main() {
int count, idx, max_val, max_idx;
scanf("%d", &count);
int arr[count];
for (idx = 0; idx < count; idx++) {
scanf("%d", &arr[idx]);
}
max_val = arr[0];
max_idx = 0;
for (idx = 1; idx < count; idx++) {
if (arr[idx] > max_val) {
max_val = arr[idx];
max_idx = idx;
}
}
printf("%d %d", max_val, max_idx);
return 0;
}
Reversing Array Elements
Storing data in an array and reversing the sequence in place before outputting the result.
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
int data[n];
for (i = 0; i < n; i++) {
scanf("%d", &data[i]);
}
int left = 0, right = n - 1;
while (left < right) {
int temp = data[left];
data[left] = data[right];
data[right] = temp;
left++;
right--;
}
for (i = 0; i < n; i++) {
if (i > 0) printf(" ");
printf("%d", data[i]);
}
return 0;
}
Identifying Unique Elements Across Arrays
Extracting elements that exist in one array but not the other, ensuring no duplicates are printed in the final output.
#include <stdio.h>
int is_in_array(int val, int arr[], int size) {
for (int i = 0; i < size; i++) {
if (val == arr[i]) return 1;
}
return 0;
}
int is_already_printed(int val, int printed[], int p_size) {
for (int i = 0; i < p_size; i++) {
if (val == printed[i]) return 1;
}
return 0;
}
int main() {
int size1, size2;
scanf("%d", &size1);
int list1[size1];
for (int i = 0; i < size1; i++) scanf("%d", &list1[i]);
scanf("%d", &size2);
int list2[size2];
for (int i = 0; i < size2; i++) scanf("%d", &list2[i]);
int unique[size1 + size2];
int u_count = 0;
for (int i = 0; i < size1; i++) {
if (!is_in_array(list1[i], list2, size2) && !is_already_printed(list1[i], unique, u_count)) {
unique[u_count++] = list1[i];
}
}
for (int i = 0; i < size2; i++) {
if (!is_in_array(list2[i], list1, size1) && !is_already_printed(list2[i], unique, u_count)) {
unique[u_count++] = list2[i];
}
}
for (int i = 0; i < u_count; i++) {
if (i > 0) printf(" ");
printf("%d", unique[i]);
}
return 0;
}
Matrix Computation Excluding Boundaries
Calculating the sum of matrix elements while excluding the last row, the last column, and the secondary diagonal.
#include <stdio.h>
int main() {
int dimension, r, c, total = 0;
scanf("%d", &dimension);
int matrix[dimension][dimension];
for (r = 0; r < dimension; r++) {
for (c = 0; c < dimension; c++) {
scanf("%d", &matrix[r][c]);
}
}
for (r = 0; r < dimension; r++) {
for (c = 0; c < dimension; c++) {
if (r == dimension - 1 || c == dimension - 1) continue;
if (r + c == dimension - 1) continue;
total += matrix[r][c];
}
}
printf("%d", total);
return 0;
}
Circular Right Shift of Square Matrix
Shifting all columns of a square matrix to the right by a specified number of positions, wrapping around the boundaries.
#include <stdio.h>
int main() {
int shift, dim, r, c;
scanf("%d %d", &shift, &dim);
int mat[dim][dim];
for (r = 0; r < dim; r++) {
for (c = 0; c < dim; c++) {
scanf("%d", &mat[r][c]);
}
}
shift = shift % dim;
for (r = 0; r < dim; r++) {
for (c = 0; c < dim; c++) {
int source_col = (c - shift + dim) % dim;
if (c > 0) printf(" ");
printf("%d", mat[r][source_col]);
}
printf("\n");
}
return 0;
}
Day of the Year Calculation
Determining the sequential day number within a year from a given date, accounting for leap years.
#include <stdio.h>
int is_leap(int yr) {
return (yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0);
}
int main() {
int year, month, day;
scanf("%d/%d/%d", &year, &month, &day);
int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (is_leap(year)) {
days_in_month[2] = 29;
}
int day_of_year = 0;
for (int m = 1; m < month; m++) {
day_of_year += days_in_month[m];
}
day_of_year += day;
printf("%d", day_of_year);
return 0;
}
Locating Character Last Occurrence
Searching a string from the end to find the last index of a target character.
#include <stdio.h>
#include <string.h>
int main() {
char target;
char text[81];
scanf("%c", &target);
getchar();
gets(text);
int len = strlen(text);
int found_idx = -1;
for (int i = len - 1; i >= 0; i--) {
if (text[i] == target) {
found_idx = i;
break;
}
}
if (found_idx == -1) {
printf("Not Found");
} else {
printf("index = %d", found_idx);
}
return 0;
}
String Reversal
Inverting the character sequence of an input string directly in memory.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}
printf("%s\n", str);
return 0;
}