简体   繁体   English

我如何用 C 中的单词创建一个“矩阵”

[英]How do i create a "matrix" with words in C

The problem is the following: Have to check if the words in the matrix are palindromes or not.问题如下:必须检查矩阵中的单词是否为回文。 Have to read the words as well.还得读单词。 My main problem is introducing the words because until now I could only do a function reading the first letter, not the whole word.我的主要问题是介绍单词,因为直到现在我只能做 function 阅读第一个字母,而不是整个单词。 After that, I think that I can check alone if it is a palindrome or not.在那之后,我认为我可以单独检查它是否是回文。 This is an example of data:这是一个数据示例:

mat←["ac" "lup" ]
    ["ou" "lupul"]
    ["ABBA" "greu" ]
m←3 number of rows
n←2 number of columns

This what I wrote until now: A function where you introduce the words:这是我写到现在的内容:A function,你在其中介绍了这些话:


    char** read(int *m ,int *n)
{
    printf("No of lines=");
    scanf("%d",m);
    printf("No of columns=");
    scanf("%d",n);

    char **a=(char**)malloc(*m*sizeof(char*));

    for (int i=0;i<*m;i++)
    {
        a[i]=(char*)malloc(*n*sizeof(char));
        for (int j=0; j<*n; j++)
        {
            fflush(stdin);
            printf("Element [%d][%d]=",i,j);
            gets(a[i]+j); // <=>  &a[i][j]
        }
    }
    return a;
}

Another one which displays it:另一个显示它的:

void display(char **x, int m, int n)
{
    for (int i=0; i<m; i++)
    {
        for (int j=0; j<n; j++)
            printf("%c ",x[i][j]);
        printf("\n");
    }
}

Another one which deletes the data:另一个删除数据的:

void freematrix(char **x, int m)
{
    for (int i=0; i<m; i++)
        free(x[i]);
    free(x);
}

This is the main part:这是主要部分:

int main()
{
    int m, n;
    char **mat;
    mat=read(&m,&n);
    display(mat,m,n);
    freematrix(mat,m);
    return 0;
}

Try this:尝试这个:

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

#define MAX_ROWS    8   // to limit the usecase
#define MAX_COLS    8
#define MAX_MAT_STR_LEN 15 //

char* str_dupe (const char* str, const int max_slen)
{
    if (!str || max_slen < 1) return NULL;
    char* dupe = malloc (max_slen + 1);
    if (!dupe) {
        perror("\nERROR: str_dupe-malloc");
        return NULL;
    }
    dupe[max_slen] = '\0'; // guard
    for (int ci =0; ci < max_slen; ++ci) {
        dupe[ci] = str[ci];
        if ('\0' == dupe[ci])
            break;  // stop copying
    }
    return dupe;
}


void free_strMatrix (char ***sm, const int rows, const int cols)
{
    if (!sm || rows < 1 || cols < 1) return;

    for (int ri = 0; ri < rows; ++ri) {
        if (sm[ri]) {
            for (int ci = 0; ci < cols; ++ci) {
                if (sm[ri][ci]) { // check if it's NULL
                    free (sm[ri][ci]);
                    sm[ri][ci] = NULL;  //prevent dangling pointers
                }
            }
            free (sm[ri]); // row of string pointers
            sm[ri] = NULL; // good practice
        }
    }
    free (sm);
}


char*** read_strMatrix (int *rows, int *cols)
{
    while (1) {
        printf ("\nChoose Rows [1..%d]: ", MAX_ROWS);
        if (1 == scanf("%d", rows) && *rows > 0 && *rows <= MAX_ROWS)
            break;
        else
            printf ("\nERROR: Invalid Row-Count. Try Again!");
    }
    while (1) {
        printf ("\nChoose Columns [1..%d]: ", MAX_COLS);
        if (1 == scanf("%d", cols) && *cols > 0 && *cols <= MAX_COLS)
            break;
        else
            printf ("\nERROR: Invalid Column-Count. Try Again!");
    }

    char*** sm = (char***) calloc ((*rows), sizeof (char**));
    if (NULL == sm) {
        perror("read_strMatrix-malloc-1");
        return NULL;
    }
    for (int ri = 0; ri < *rows; ++ri) {
        sm[ri] = (char**) calloc ((*cols), sizeof (char*));
        if (NULL == sm[ri]) {
            perror ("read_strMatrix-malloc-2");
            //ideally you should free allocated memory before return
            free_strMatrix(sm, *rows, *cols);
            return NULL;
        }
    }
    char str[256]; //interim string buffer;
    for (int ri = 0; ri < *rows; ++ri) {
        for (int ci=0; ci < *cols; ++ci ) {
            printf ("String for strMatrix[%d][%d] : ", ri, ci);
            // strings more than 255 chars will be split ; so be within limit duing input
            while (1 != scanf ("%255s", str));
            // only copying MAX_MAT_STR_LEN chars
            sm[ri][ci] = str_dupe (str, MAX_MAT_STR_LEN);
            if (NULL == sm[ri][ci]) {
                perror("read_strMatrix-strndup");
                //ideally you should free allocated memory before return
                free_strMatrix (sm, *rows, *cols);
                return NULL;
            }
        }
        printf ("\n");
    }
    return sm;
}


void disp_strMatrix (char ***sm, const int rows, const int cols)
{
    if (!sm || rows < 1 || cols < 1) return;
    printf ("\nStringMatrix [%d][%d]:\n", rows, cols);

    for (int ri = 0; ri < rows; ++ri) {
        if (sm[ri]) {
            for (int ci = 0; ci < cols; ++ci)
                printf ("%s\t", sm[ri][ci]);
        }
        printf ("\n");
    }
    printf ("\n");
}


int main()
{
    int rows, cols;
    char ***strMatrix;
    strMatrix = read_strMatrix (&rows, &cols);
    if (!strMatrix) {
        printf ("\nERROR: reading strMatrix\n");
        return 1;
    }
    disp_strMatrix (strMatrix, rows, cols);
    free_strMatrix (strMatrix, rows, cols);
    strMatrix = NULL; // good practice

    return 0;
}

You can also prepare an input.txt file like:您还可以准备一个input.txt文件,例如:

3 4
aaaaa   sssss   ffg gggg    
hhhh    jj  kkkkkk  lllll   
qqq wwwww   eeeee   rrrrr

On Linux you can run the program like:在 Linux 上,您可以像这样运行程序:

./a.out < input.txt 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM