Append Alphabet – Surrounding Integers
Given a matrix containing integers and alphabets, append each alphabet to all surrounding integers in the 3x3 neighborhood.
Understand the Problem
Problem Statement
The program must accept a matrix of size R*C containing integers and alphabets as the input. For each occurrence of the alphabet in the matrix, the program must append the alphabet to the surrounding integers of the alphabet. Then the program must print the revised matrix as the output.
Note: In the given matrix, there are no common integers around two or more alphabets.
Constraints
- 2 ≤ R, C ≤ 50
- 1 ≤ Each integer value in the matrix ≤ 1000
- The matrix contains only integers and alphabets
- Alphabets are surrounded by integers (no edge cases where alphabets are at matrix boundaries without integers)
- No integer is surrounded by multiple alphabets
Examples
4 5
5 2 2 2 4
7 5 2 3 b
8 a 5 5 9
9 3 8 5 65 2 2 2b 4b
7a 5a 2a 3b b
8a a 5a 5b 9b
9a 3a 8a 5 6The alphabet 'b' at position (1,4) appends itself to surrounding integers: 2, 4, 3, 5, and 9, making them 2b, 4b, 3b, 5b, and 9b. The alphabet 'a' at position (2,1) appends itself to surrounding integers: 7, 5, 2, 8, 3, and 8, making them 7a, 5a, 2a, 8a, 3a, and 8a.
5 5
86 77 45 57 82
13 A 10 86 70
32 74 72 27 C
28 79 12 43 90
15 B 13 15 3786A 77A 45A 57 82
13A A 10A 86C 70C
32A 74A 72A 27C C
28B 79B 12B 43C 90C
15B B 13B 15 37The alphabet 'A' at position (1,1) appends itself to surrounding integers, making them 86A, 77A, 45A, 13A, 10A, 32A, 74A, and 72A. The alphabet 'C' at position (2,4) appends itself to surrounding integers: 86C, 70C, 27C, 43C, and 90C. The alphabet 'B' at position (4,1) appends itself to surrounding integers: 28B, 79B, 12B, 15B, and 13B.
Solution
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
bool isAlpha(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
int main() {
int rows, cols;
scanf("%d %d", &rows, &cols);
// Matrix storage: [rows][cols][max_string_length]
// Assuming max length of 100 characters for safety
char matrix[rows][cols][100];
// Read input matrix
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%s", matrix[i][j]);
}
}
// Process each cell
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
// Check if current cell contains an alphabet
if(isAlpha(matrix[row][col][0])) {
char alphabet = matrix[row][col][0];
// Process 3x3 neighborhood
for(int x_offset = -1; x_offset <= 1; x_offset++) {
for(int y_offset = -1; y_offset <= 1; y_offset++) {
int new_row = row + x_offset;
int new_col = col + y_offset;
// Check bounds
if(new_row >= 0 && new_row < rows &&
new_col >= 0 && new_col < cols) {
// Check if neighbor is an integer (first char is digit)
if(isdigit(matrix[new_row][new_col][0])) {
// Append alphabet to the integer string
matrix[new_row][new_col][strlen(matrix[new_row][new_col])] = alphabet;
matrix[new_row][new_col][strlen(matrix[new_row][new_col]) + 1] = '\0';
}
}
}
}
// Replace alphabet cell with just the alphabet character
matrix[row][col][0] = alphabet;
matrix[row][col][1] = '\0';
}
}
}
// Print result
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%s ", matrix[i][j]);
}
printf("\n");
}
return 0;
}C Solution Explanation:
- Matrix Storage: Uses a 3D char array to store the matrix, with each cell capable of holding strings up to 100 characters
- Input Reading: Reads the matrix using scanf with %s format specifier
- Alphabet Detection: Uses isAlpha() helper function and checks first character of each cell
- Neighborhood Processing: For each alphabet, iterates through the 3x3 neighborhood using nested loops with offsets (-1 to 1)
- Bounds Checking: Ensures we don't access cells outside matrix boundaries
- Integer Detection: Uses isdigit() to check if a neighboring cell contains an integer
- String Appending: Manually appends the alphabet character to integer strings by manipulating string length
- Output: Prints the modified matrix with proper spacing