Implementing Saddle Point Search and String Operations in C

Finding Saddle Points in a 2D Array

A saddle point in an m×n matrix is an element that is both the maximum in its row and the minimum in its column.

#include <stdio.h>

void locateSaddlePoint(int matrix[100][100], int rows, int cols) {
    for (int row = 0; row < rows; row++) {
        int rowMax = matrix[row][0];
        int maxCol = 0;
        
        // Find maximum element in current row
        for (int col = 1; col < cols; col++) {
            if (matrix[row][col] > rowMax) {
                rowMax = matrix[row][col];
                maxCol = col;
            }
        }
        
        // Verify if it's minimum in its column
        int isSaddle = 1;
        for (int k = 0; k < rows; k++) {
            if (matrix[k][maxCol] < rowMax) {
                isSaddle = 0;
                break;
            }
        }
        
        if (isSaddle) {
            printf("Matrix[%d][%d]=%d", row, maxCol, rowMax);
        }
    }
}

int main() {
    int m, n;
    scanf("%d %d", &m, &n);
    
    int arr[100][100];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &arr[i][j]);
        }
    }
    
    locateSaddlePoint(arr, m, n);
    return 0;
}

Binary Search Implementation

Binary search efficiently locates a target value in a sorted array using a divide-and-conquer approach.

#include <stdio.h>

int binarySearch(int* array, int size, int target) {
    int low = 0;
    int high = size - 1;
    
    while (low <= high) {
        int mid = low + (high - low) / 2;
        
        if (array[mid] == target) {
            return mid;
        } else if (array[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    
    return -1;
}

int main() {
    int numbers[] = {-1, 0, 3, 5, 9, 12};
    int searchValue;
    int arraySize = sizeof(numbers) / sizeof(numbers[0]);
    
    scanf("%d", &searchValue);
    int position = binarySearch(numbers, arraySize, searchValue);
    
    if (position != -1) {
        printf("%d\n", position);
    } else {
        printf("-1\n");
    }
    
    return 0;
}

Sorting Strings Lexicographical

This program sorts three strings in ascending alphabetical order.

#include <stdio.h>
#include <string.h>

void exchangeStrings(char* first, char* second) {
    char buffer[100];
    strcpy(buffer, first);
    strcpy(first, second);
    strcpy(second, buffer);
}

int main() {
    char str1[100], str2[100], str3[100];
    scanf("%s\n%s\n%s", str1, str2, str3);
    
    if (strcmp(str1, str2) > 0) {
        exchangeStrings(str1, str2);
    }
    if (strcmp(str1, str3) > 0) {
        exchangeStrings(str1, str3);
    }
    if (strcmp(str2, str3) > 0) {
        exchangeStrings(str2, str3);
    }
    
    printf("%s\n%s\n%s\n", str1, str2, str3);
    return 0;
}

String Insertion Operation

Inserts one string into another at a specified position.

#include <stdio.h>
#include <string.h>

int main() {
    char primary[100], secondary[100], result[200];
    int position;
    
    scanf("%s%s%d", primary, secondary, &position);
    
    // Copy prefix up to insertion point
    for (int i = 0; i < position; i++) {
        result[i] = primary[i];
    }
    result[position] = '\0';
    
    // Append the secondary string
    strcat(result, secondary);
    
    // Extract suffix after insertion point
    for (int i = position; primary[i] != '\0'; i++) {
        secondary[i - position] = primary[i];
    }
    secondary[strlen(primary) - position] = '\0';
    
    // Append the remaining suffix
    strcat(result, secondary);
    
    printf("%s", result);
    return 0;
}

String Length Analysis with Stop Condition

Processes strings until "stop" is encountreed, calculating total length and finding the longest word.

#include <stdio.h>
#include <string.h>

int main() {
    char input[1000];
    char longestWord[1000];
    
    gets(input);
    
    while (strcmp(input, "stop") != 0) {
        int totalLength = 0;
        int currentLength = 0;
        int maxLength = 0;
        
        // Calculate total length and find maximum word length
        for (int i = 0; i < strlen(input); i++) {
            if (input[i] != ' ') {
                totalLength++;
                currentLength++;
            } else {
                if (currentLength > maxLength) {
                    maxLength = currentLength;
                }
                currentLength = 0;
            }
        }
        
        if (currentLength > maxLength) {
            maxLength = currentLength;
        }
        
        currentLength = 0;
        
        // Locate the first occurrence of the longest word
        for (int i = 0; i < strlen(input); i++) {
            if (input[i] != ' ') {
                currentLength++;
                if (currentLength == maxLength) {
                    for (int j = 0; j < maxLength; j++) {
                        longestWord[j] = input[j + i - maxLength + 1];
                    }
                    longestWord[maxLength] = '\0';
                    break;
                }
            } else {
                currentLength = 0;
            }
        }
        
        printf("%d %s\n", totalLength, longestWord);
        gets(input);
    }
    
    return 0;
}

Tags: c programming algorithms String Manipulation Binary Search Array Processing

Posted on Thu, 14 May 2026 11:36:24 +0000 by HairyArse