C Programming Techniques for String Analysis and Array Transformations
Palindrome Validation with Bidirectional Traversal
Determining whether a sequence reads identically from both directions can be achieved without auxiliary buffers by employing converging indices:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define BUFFER_SIZE 100
bool is_symmetric(const char *sequence) {
const char *forward = sequence;
const char *backward = sequence + strlen(sequence) - 1;
while (forward < backward) {
if (*forward != *backward) {
return false;
}
forward++;
backward--;
}
return true;
}
int main(void) {
char input[BUFFER_SIZE];
printf("Enter text: ");
if (fgets(input, BUFFER_SIZE, stdin)) {
input[strcspn(input, "\n")] = 0;
printf("%s\n", is_symmetric(input) ? "Palindrome" : "Not palindrome");
}
return 0;
}
Structured Month Lookup with Festival Data
Organizing temporal data using aggregate types improves maintainability compared to parallel arrays:
#include <stdio.h>
#include <string.h>
#define MONTH_COUNT 12
#define NAME_LENGTH 20
typedef struct {
char standard[NAME_LENGTH];
char abbreviated[NAME_LENGTH];
char celebration[NAME_LENGTH];
} CalendarMonth;
CalendarMonth year_data[MONTH_COUNT] = {
{"January", "Jan.", "New Year Celebration"},
{"February", "Feb.", "Spring Festival"},
{"March", "Mar.", "Arbor Day"},
{"April", "Apr.", "Qingming Festival"},
{"May", "May.", "Labor Day"},
{"June", "Jun.", "Children's Day"},
{"July", "Jul.", "Qixi Festival"},
{"August", "Aug.", "Mid-Autumn Festival"},
{"September", "Sept.", "School Opening"},
{"October", "Oct.", "National Day"},
{"November", "Nov.", "Singles' Day"},
{"December", "Dec.", "Christmas"}
};
int locate_month(const char *query) {
for (int idx = 0; idx < MONTH_COUNT; idx++) {
if (strcmp(year_data[idx].standard, query) == 0 ||
strcmp(year_data[idx].abbreviated, query) == 0) {
return idx;
}
}
return -1;
}
int main(void) {
char user_input[NAME_LENGTH];
printf("Input month name: ");
scanf("%s", user_input);
int position = locate_month(user_input);
if (position >= 0) {
printf("%s: %s\n", user_input, year_data[position].celebration);
} else {
printf("Month not recognized\n");
}
return 0;
}
Filtering Numeric Characters In-Place
Eliminating non-digit symbols efficiently by maintaining separate read and write positions within a single buffer:
#include <stdio.h>
#include <ctype.h>
#define MAX_INPUT 80
void extract_numerals(char *stream) {
char *read_ptr = stream;
char *write_ptr = stream;
while (*read_ptr) {
if (isdigit((unsigned char)*read_ptr)) {
*write_ptr++ = *read_ptr;
}
read_ptr++;
}
*write_ptr = '\0';
}
int main(void) {
char buffer[MAX_INPUT];
printf("Input string: ");
fgets(buffer, MAX_INPUT, stdin);
extract_numerals(buffer);
printf("Digits only: %s\n", buffer);
return 0;
}
Two-Dimensional Matrix Transposition
Transforming row-major data to column-major orientation using index-based access:
#include <stdio.h>
#define ORIGIN_ROWS 3
#define ORIGIN_COLS 4
void swap_dimensions(int source[][ORIGIN_COLS], int target[][ORIGIN_ROWS],
int src_rows, int src_cols) {
for (int row = 0; row < src_rows; row++) {
for (int col = 0; col < src_cols; col++) {
target[col][row] = source[row][col];
}
}
}
void populate_matrix(int matrix[][ORIGIN_COLS], int rows, int cols) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
scanf("%d", &matrix[r][c]);
}
}
}
void display_matrix(int matrix[][ORIGIN_ROWS], int rows, int cols) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
printf("%d\t", matrix[r][c]);
}
printf("\n");
}
}
int main(void) {
int original[ORIGIN_ROWS][ORIGIN_COLS];
int transposed[ORIGIN_COLS][ORIGIN_ROWS];
populate_matrix(original, ORIGIN_ROWS, ORIGIN_COLS);
swap_dimensions(original, transposed, ORIGIN_ROWS, ORIGIN_COLS);
display_matrix(transposed, ORIGIN_COLS, ORIGIN_ROWS);
return 0;
}
Lexicographical Ordering of String Arrays
Arranging string collections alphabetically using selection sort on pointer arrays:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LIMIT 20
#define CHAR_LIMIT 50
void order_strings(char **collection, int total) {
for (int current = 0; current < total - 1; current++) {
int smallest = current;
for (int compare = current + 1; compare < total; compare++) {
if (strcmp(collection[compare], collection[smallest]) < 0) {
smallest = compare;
}
}
if (smallest != current) {
char *temporary = collection[current];
collection[current] = collection[smallest];
collection[smallest] = temporary;
}
}
}
int main(void) {
char storage[STRING_LIMIT][CHAR_LIMIT];
char *references[STRING_LIMIT];
int quantity;
printf("Enter count (max %d): ", STRING_LIMIT);
scanf("%d", &quantity);
getchar();
for (int i = 0; i < quantity; i++) {
printf("String %d: ", i);
fgets(storage[i], CHAR_LIMIT, stdin);
storage[i][strcspn(storage[i], "\n")] = 0;
references[i] = storage[i];
}
order_strings(references, quantity);
printf("Sorted results:\n");
for (int i = 0; i < quantity; i++) {
printf("%d: %s\n", i, references[i]);
}
return 0;
}
Optimized Circular Array Rotation
Achieving O(n) time complexity with O(1) auxiliary space through the reversal algorithm:
#include <stdio.h>
void reverse_segment(int *data, int low, int high) {
while (low < high) {
int temporary = data[low];
data[low] = data[high];
data[high] = temporary;
low++;
high--;
}
}
void circular_shift(int *elements, int length, int offset) {
offset = offset % length;
reverse_segment(elements, 0, length - 1);
reverse_segment(elements, 0, offset - 1);
reverse_segment(elements, offset, length - 1);
}
int main(void) {
int count, shift;
scanf("%d %d", &count, &shift);
int values[100];
for (int i = 0; i < count; i++) {
scanf("%d", &values[i]);
}
circular_shift(values, count, shift);
for (int i = 0; i < count; i++) {
printf("%d%c", values[i], (i < count - 1) ? ' ' : '\n');
}
return 0;
}
Josephus Problem Solutions
Array Index Simulation
Tracking elimination using modular arithmetic without pointer indiretcion:
#include <stdio.h>
int find_survivor(int participants) {
int circle[100] = {0};
int alive = participants;
int position = 0;
int tally = 0;
while (alive > 1) {
if (circle[position] == 0) {
tally++;
if (tally == 3) {
circle[position] = 1;
alive--;
tally = 0;
}
}
position = (position + 1) % participants;
}
for (int i = 0; i < participants; i++) {
if (circle[i] == 0) return i + 1;
}
return 0;
}
int main(void) {
int n;
scanf("%d", &n);
printf("Survivor: %d\n", find_survivor(n));
return 0;
}
Dynamic Memory with Compression
Allocating flexible storage and compacting the array after each removal:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int total, step = 3, cursor = 0;
scanf("%d", &total);
int *players = malloc(total * sizeof(int));
for (int i = 0; i < total; i++) {
players[i] = i + 1;
}
int remaining = total;
while (remaining > 1) {
cursor = (cursor + step - 1) % remaining;
for (int i = cursor; i < remaining - 1; i++) {
players[i] = players[i + 1];
}
remaining--;
}
printf("Final position: %d\n", players[0]);
free(players);
return 0;
}
Pointer-Based Elimination Marking
Static array manipulation using pointer traversal to nullify eliminated entries:
#include <stdio.h>
int main(void) {
int count;
scanf("%d", &count);
int group[100];
int *ptr = group;
for (int i = 0; i < count; i++) {
*(ptr + i) = i + 1;
}
int eliminated = 0;
int calls = 0;
int offset = 0;
while (eliminated < count - 1) {
if (*(ptr + offset) != 0) {
calls++;
if (calls == 3) {
*(ptr + offset) = 0;
eliminated++;
calls = 0;
}
}
offset = (offset + 1) % count;
}
for (int i = 0; i < count; i++) {
if (*(ptr + i) != 0) {
printf("Remaining: %d\n", *(ptr + i));
}
}
return 0;
}