我正在嘗試制作一個接受字串和密鑰的程式。鍵確定矩陣中的列數。例如,如果字串是 hello there 并且鍵是 BYE 輸出應該是:(_代表空格)
h e l
l o _
t h e
r e _
這是我的代碼。我無法列印矩陣。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(){
char key[50];
char line[256];
printf("Enter your string:");
if (fgets(line, sizeof line, stdin) == NULL) {
fprintf(stderr, "No line read\n");
exit(EXIT_FAILURE);
}
printf("Enter your key");
if (fgets(key, sizeof key, stdin) == NULL) {
fprintf(stderr, "No line read\n");
exit(EXIT_FAILURE);
}
int len = strlen(line);
if (len && line[len - 1] == '\n')
line[--len] = '\0';
int len1 = strlen(key);
if (len1 && key[len1 - 1] == '\n')
key[--len]= '\0';
printf("%s\n", line);
printf("%s\n", key);
//ascendingOrderString(key);
gridStr(line,key);
}
void gridStr(char *line, char *key)
{
char mat[10][10];
int columns = strlen(key)-1;
char wordR;
int rows = 0;
int i,j = 0;
while (line[i]) {
putchar(line[i]);
mat[rows][columns] = line[i ];
if (i % columns == 0) putchar('\n');
}
if (i % columns != 0) putchar('\n');
printf("%s", mat[i][j]);
}
}
uj5u.com熱心網友回復:
從代碼中洗掉附加內容}并添加原型定義后,gridStr我看到了以下挑戰:
$ gcc -Wall hack.c
hack.c:53:18: warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
printf("%s", mat[i][j]);
~~ ^~~~~~~~~
%c
hack.c:42:10: warning: unused variable 'wordR' [-Wunused-variable]
char wordR;
^
hack.c:46:17: warning: variable 'i' is uninitialized when used here [-Wuninitialized]
while (line[i]) {
^
hack.c:44:10: note: initialize the variable 'i' to silence this warning
int i,j = 0;
^
= 0
3 warnings generated.
$
所以,讓我們用替換%s和%c初始化。i0
$ gcc -Wall hack.c
hack.c:42:10: warning: unused variable 'wordR' [-Wunused-variable]
char wordR;
^
1 warning generated.
$ ./a.out
Enter your string:hello there
Enter your keyBYE
hello there
BYE
hel
lo
the
re
u%
$
看起來我們快到了。該%符號表示應用程式末尾沒有列印換行符。它前面的“u”表示由于i遞增而列印未初始化的值。
mat[rows][columns] = line[i ];更新總是相同的元素。這當然不是故意的。
去掉wordR,輸出'_'而不是'',填充矩陣:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <math.h>
void gridStr(char *line, char *key);
int main(void) {
char key[50];
char line[256];
printf("Enter your string: ");
if (fgets(line, sizeof line, stdin) == NULL) {
fprintf(stderr, "No line read\n");
exit(EXIT_FAILURE);
}
printf("Enter your key: ");
if (fgets(key, sizeof key, stdin) == NULL) {
fprintf(stderr, "No line read\n");
exit(EXIT_FAILURE);
}
int len = strlen(line);
if (len && line[len - 1] == '\n')
line[--len] = '\0';
int len1 = strlen(key);
if (len1 && key[len1 - 1] == '\n')
key[--len]= '\0';
printf("%s\n", line);
printf("%s\n", key);
//ascendingOrderString(key);
gridStr(line, key);
}
void gridStr(char *line, char *key)
{
char mat[10][10] = {0};
int columns = strlen(key)-1;
int rows = 0;
int i=0,j = 0;
while (line[i]) {
if (line[i] == ' ') {
putchar('_');
} else {
putchar(line[i]);
}
mat[rows][i % columns] = line[i];
i ;
if (i > 0 && i % columns == 0) {
putchar('\n');
rows ;
}
}
if (i % columns != 0) putchar('\n');
rows ; // from current row to number of rows
printf("\nMatrix:\n");
for (i = 0; i < rows; i ) {
for (j = 0; j < columns; j ) {
if (mat[i][j] == ' ') {
putchar('_');
} else {
putchar(mat[i][j]);
}
}
printf("\n");
}
}
現在我們開始:
$ gcc -Wall hack.c
$ ./a.out
Enter your string: hello there
Enter your key: BYE
hello there
BYE
hel
lo_
the
re
Matrix:
hel
lo_
the
re
$
直接列印輸出和矩陣輸出匹配。完畢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487163.html
標籤:C
