Append Alphabet – Surrounding Integers

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.

Boundary Condition(s):
2 <= R, C <= 50
1 <= Each integer value in the matrix <= 1000

Input Format:
The first line contains R and C separated by a space.
The next R lines, each contains C elements of the matrix separated by a space.

Output Format:
The first R lines contain the revised matrix.

Example Input/Output 1:
Input:
4 5
5 2 2 2 4
7 5 2 3 b
8 a 5 5 9
9 3 8 5 6

Output:
5 2 2 2b 4b
7a 5a 2a 3b b
8a a 5a 5b 9b
9a 3a 8a 5 6

Explanation:
Here R=4 and C=5.
The alphabets in the given matrix are highlighted below
5 2 2 2 4
7 5 2 3 b
a 5 5 9
9 3 8 5 6
After modifying the matrix based on the given conditions, the matrix becomes
5 2 2 2b 4b
7a 5a 2a 3b b
8a a 5a 5b 9b
9a 3a 8a 5 6

Example Input/Output 2:
Input:
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 37

Output:
86A 77A 45A 57 82
13A A 10A 86C 70C
32A 74A 72A 27C C
28B 79B 12B 43C 90C
15B B 13B 15 37

rows, cols = map(int, input().split())
matrix = [input().split() for _ in range(rows)]
for row in range(rows):
    for col in range(cols):
        current_char = matrix[row][col]
        if current_char.isalpha():
            for x_offset in range(-1, 2):
                for y_offset in range(-1, 2):
                    if 0 <= row + x_offset < rows and 0 <= col + y_offset < cols:
                        matrix[row + x_offset][col + y_offset] += current_char
            matrix[row][col] = current_char
for row_content in matrix:
    print(*row_content)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isAlpha(char c) {
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

int main() {
    int rows, cols;
    scanf("%d %d", &rows, &cols);
    char matrix[rows][cols][5]; // Assuming maximum 4 characters + null terminator

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            scanf("%s", matrix[i][j]);
        }
    }

    for(int row = 0; row < rows; row++) {
        for(int col = 0; col < cols; col++) {
            if(isAlpha(matrix[row][col][0])) {
                for(int x_offset = -1; x_offset <= 1; x_offset++) {
                    for(int y_offset = -1; y_offset <= 1; y_offset++) {
                        if(row + x_offset >= 0 && row + x_offset < rows && col + y_offset >= 0 && col + y_offset < cols) {
                            strcat(matrix[row + x_offset][col + y_offset], matrix[row][col]);
                        }
                    }
                }
                strcpy(matrix[row][col], &matrix[row][col][strlen(matrix[row][col]) - 1]);
            }
        }
    }

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            printf("%s ", matrix[i][j]);
        }
        printf("n");
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    int rows, cols;
    cin >> rows >> cols;
    vector<vector<string>> matrix(rows, vector<string>(cols));

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            cin >> matrix[i][j];
        }
    }

    for(int row = 0; row < rows; row++) {
        for(int col = 0; col < cols; col++) {
            if(isalpha(matrix[row][col][0])) {
                for(int x_offset = -1; x_offset <= 1; x_offset++) {
                    for(int y_offset = -1; y_offset <= 1; y_offset++) {
                        if(row + x_offset >= 0 && row + x_offset < rows && col + y_offset >= 0 && col + y_offset < cols) {
                            matrix[row + x_offset][col + y_offset] += matrix[row][col];
                        }
                    }
                }
                matrix[row][col] = matrix[row][col].back();
            }
        }
    }

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
import java.util.Scanner;

public class MatrixManipulation {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        int cols = sc.nextInt();
        String[][] matrix = new String[rows][cols];

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                matrix[i][j] = sc.next();
            }
        }

        for(int row = 0; row < rows; row++) {
            for(int col = 0; col < cols; col++) {
                if(Character.isLetter(matrix[row][col].charAt(0))) {
                    for(int x_offset = -1; x_offset <= 1; x_offset++) {
                        for(int y_offset = -1; y_offset <= 1; y_offset++) {
                            if(row + x_offset >= 0 && row + x_offset < rows && col + y_offset >= 0 && col + y_offset < cols) {
                                matrix[row + x_offset][col + y_offset] += matrix[row][col];
                            }
                        }
                    }
                    matrix[row][col] = matrix[row][col].substring(matrix[row][col].length() - 1);
                }
            }
        }

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Previous Article

Swap - Multiples of X and Y

Next Article

Two Matrix Spiral Print

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *