Practical C Function Implementations
Basic Arithmetic Evaluator
Construct a function that evaluates a simple mathematical expression given two integer operands and a character operator.
c #include <stdio.h>
int compute(int val1, int val2, char op);
int main() { int x, y; char sym; printf("Enter expression (e.g., 5+3): "); scanf("%d%c%d", &x, &sym, &y); int outcome = compute(x, y, sym); printf("Result: %d\n", outcome); return 0; }
int compute(int val1, int val2, char op) { if (op == '+') return val1 + val2; if (op == '-') return val1 - val2; if (op == '*') return val1 * val2; if (op == '/') return val1 / val2; if (op == '%') return val1 % val2; return 0; }
Prime Number Verification
Design a routine that checks whether an input integer is a prime number, returning 1 for true and 0 for false.
c #include <stdio.h> #include <math.h>
int checkPrime(int num);
int main() { int num; printf("Enter an integer: "); scanf("%d", &num); printf("%d\n", checkPrime(num)); return 0; }
int checkPrime(int num) { if (num <= 1) return 0; int divisor = 2; while (divisor * divisor <= num) { if (num % divisor == 0) { return 0; } divisor++; } return 1; }
Array Element Accumulator
Write a function that takes an integer array and its size, returning the sum of all its elements.
c #include <stdio.h>
int accumulate(int arr[], int size);
int main() { int dataset[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int total = accumulate(dataset, 10); printf("Total: %d\n", total); return 0; }
int accumulate(int arr[], int size) { int total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total; }
Optimized Bubble Sort
Implement the bubble sort algorithm with an early-exit optimization if no swaps occur during a pass.
c #include <stdio.h>
void sortArray(int data[], int len);
int main() { int data[] = {20, 40, 10, 5, 50, 30}; sortArray(data, 6); for (int i = 0; i < 6; i++) { printf("%d ", data[i]); } return 0; }
void sortArray(int data[], int len) { for (int i = 0; i < len - 1; i++) { int swapped = 0; for (int j = 0; j < len - i - 1; j++) { if (data[j] > data[j + 1]) { int temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; swapped = 1; } } if (!swapped) break; } }
Character Frequency Analyzer
Create a function that categorizes characters in a string into uppercase letters, lowercase letters, digits, spaces, and other characters, tallying each category.
c #include <stdio.h>
void analyzeChars(const char text[], int counts[]);
int main() { char text[100]; int counts[5] = {0}; // upper, lower, digit, space, other printf("Enter a string: "); fgets(text, sizeof(text), stdin); analyzeChars(text, counts); printf("Uppercase: %d\n", counts[0]); printf("Lowercase: %d\n", counts[1]); printf("Digits: %d\n", counts[2]); printf("Spaces: %d\n", counts[3]); printf("Others: %d\n", counts[4]); return 0; }
void analyzeChars(const char text[], int counts[]) { int i = 0; while (text[i] != '\0') { if (text[i] >= 'A' && text[i] <= 'Z') counts[0]++; else if (text[i] >= 'a' && text[i] <= 'z') counts[1]++; else if (text[i] >= '0' && text[i] <= '9') counts[2]++; else if (text[i] == ' ' || text[i] == '\t') counts[3]++; else if (text[i] != '\n') counts[4]++; i++; } }
String Length Determination
Implement a function equivalent to strlen that counts the number of characters in a string before the null terminator.
c #include <stdio.h>
int getLength(const char str[]);
int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); int len = getLength(str); if (len > 0 && str[len - 1] == '\n') str[len - 1] = '\0'; printf("Length: %d\n", getLength(str)); return 0; }
int getLength(const char str[]) { int len = 0; while (str[len] != '\0') { len++; } return len; }
Uppercase to Lowercase Conversion
Write a function that modifies a string in-place, converting all uppercase alphabetic characters to their lowercase equivalents.
c #include <stdio.h>
void toLowerCase(char str[]);
int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); toLowerCase(str); printf("Lowercased: %s\n", str); return 0; }
void toLowerCase(char str[]) { for (int i = 0; str[i] != '\0'; i++) { if (str[i] >= 'A' && str[i] <= 'Z') { str[i] += 32; } } }
String Copy Operation
Develop a function that duplicates the contents of a source character array into a destination array.
c #include <stdio.h>
void copyString(char destination[], const char source[]);
int main() { char source[100], destination[100]; printf("Enter a string: "); fgets(source, sizeof(source), stdin); copyString(destination, source); printf("Copied: %s\n", destination); return 0; }
void copyString(char destination[], const char source[]) { int i = 0; while (source[i] != '\0') { destination[i] = source[i]; i++; } destination[i] = '\0'; }
Lexicographical String Comparison
Construct a function to compare two strings lexicographically, returning 1 if the first is greater, -1 if smaller, and 0 if they are identical.
c #include <stdio.h>
int compareStrings(const char s1[], const char s2[]);
int main() { char s1[100], s2[100]; printf("Enter first string: "); fgets(s1, sizeof(s1), stdin); printf("Enter second string: "); fgets(s2, sizeof(s2), stdin); printf("Comparison result: %d\n", compareStrings(s1, s2)); return 0; }
int compareStrings(const char s1[], const char s2[]) { int i = 0; while (s1[i] != '\0' || s2[i] != '\0') { if (s1[i] > s2[i]) return 1; if (s1[i] < s2[i]) return -1; i++; } return 0; }
Prime Factorization
Write a function that decomposes an integer into its prime factors and prints the result as a multiplication sequence, e.g., 12=2*2*3.
c #include <stdio.h>
void factorize(int value);
int main() { int value; printf("Enter an integer: "); scanf("%d", &value); factorize(value); return 0; }
void factorize(int value) { int original = value; printf("%d=", original); int is_first = 1; int divisor = 2; while (value > 1) { if (value % divisor == 0) { if (!is_first) printf("*"); printf("%d", divisor); value /= divisor; is_first = 0; } else { divisor++; } } printf("\n"); }
Integer Digit Counter
Create a function that calculates the number of digits in an integer, properly handling negative values and zero.
c #include <stdio.h>
int countDigits(int number);
int main() { int number; printf("Enter an integer: "); scanf("%d", &number); printf("Digit count: %d\n", countDigits(number)); return 0; }
int countDigits(int number) { if (number == 0) return 1; if (number < 0) number = -number; int digits = 0; while (number > 0) { number /= 10; digits++; } return digits; }