[英]two dimensional array in c, reversing and storing a sentence
嘿,我真的是新手,对数组有麻烦。 有人可以帮我这个项目。 “ c用一种现代方法编程:修改一个程序以使句子中的单词反向,以便在读取句子时将其存储在二维char数组中,数组的每一行都存储一个单词。假定该句子包含不超过30个单词,且每个单词的长度不超过20个字符。请确保在每个单词的末尾存储一个空字符,以便可以将其视为字符串”(同样,我也听不懂它的意思。空字符)。 这是我的尝试,但是不起作用。 我想我虽然接近。
#include <stdio.h>
#define MAX_SENTENCE_LEN 80
#define SENTENCE_MAX 30
#define WORD_MAX 20
int main(void)
{
char ch, sentence[MAX_SENTENCE_LEN] = {' '}, terminator = '.';
int n, i, j, start, finish;
printf("Enter a sentence: ");
for (n = 1; n < MAX_SENTENCE_LEN; n++) {
ch = getchar();
if (ch == '.' || ch == '?' || ch == '!') {
terminator = ch;
break;
}
sentence[n] = ch;
}
printf("Reversal of sentence:");
finish = n;
for (start = finish - 1; start >= 0; start--) {
if (sentence[start] == ' ') {
for (i = start; i < finish; i++)
putchar(sentence[i]);
finish = start;
}
{
int sentence[SENTENCE_MAX][WORD_MAX];
int word[30][20];
for (i=0; i< SENTENCE_MAX;i++){
for (j=0; j<WORD_MAX; j++)
sentence[i][j]=-1;
}
}
}
printf("%c\n", terminator);
return 0;}
我写了一个新代码,我认为它更接近我想要的代码,但仍然无法运行。 我的编译器有问题吗? 无论如何,这是新的代码
#include<stdio.h>
#define N 100
int main (void)
{
char sentence[N][N], ch, termChar;
int i = 0, l = 0, count = 0;
int j = 0, k, start, finish, word;
printf("enter a sentence: ");
while (ch = getchar())
{
sentence[i][l++]= ch;
if (ch == ' ')
{
sentence[i][l] = '\0';
i++;
l = 0;
count++;
}
if (ch == '.' || ch == '!' || ch == '?')
{
sentence[i][l-1]= ' ';
sentence[i][l]= '\0';
termChar = ch;
count ++;
break;
}
}
for(i=count ; i>=0; i--)
printf("%s ", sentence[i]);
printf("%c\n", termChar);
return 0;
}
您的代码在我的环境(Windows,C99编译器,32位版本)中运行完美。 我输入了一个简短的句子,然后它反过来了:
关于 : 我不了解空字符的说法
C字符串由char数组末尾的空字符\\ 0定义。 示例char string [] =“ word”看起来像:| w | o | r | d | \\ 0 | 在记忆中。
没有\\0
,它将只是一个char数组,而不是一个字符串,因此无法在任何字符串函数(例如strcpy()
, strlen()
等strlen()
中使用。
顺便说一下, sentence
创建和初始化:
char sentence[MAX_SENTENCE_LEN] = {' '};
不保证char数组整个长度的内容。
这可能是您的环境没有运行代码而我的环境却在运行代码的原因。 根据编译器,操作系统和其他随机因素, sentence
可能填充任何内容。 因此,如果您的代码未在计算机上运行,则可能只需要将sentence
初始化为\\0
。 用以下内容替换该行:
char sentence[MAX_SENTENCE_LEN]; //create
memset(sentence, 0 ,MAX_SENTENCE_LEN); //zero all memory
sentence[0]=' '; //set first char to a space (' '). (not sure why)
同样偶然的是,如果用户输入的字符串长度== MAX_SENTENCE_LEN,则您的程序将崩溃,因为sentence
只有足够的空间用于MAX_SENTENCE_LEN-1 + \\0
。
#include <stdio.h>
#define MAX_SENTENCE_LEN 80
#define SENTENCE_MAX 30
#define WORD_MAX 20
int main(void){
char ch, sentence[MAX_SENTENCE_LEN] = {' '}, terminator = '.';
int n, i, j, start, finish;
char word[SENTENCE_MAX][WORD_MAX+1];
int wc=0, wcc=0;
printf("Enter a sentence: ");
for (n = 1; n < MAX_SENTENCE_LEN; n++) {
ch = getchar();
if (ch == '.' || ch == '?' || ch == '!') {
terminator = ch;
break;
} else if(ch != ' '){
word[wc][wcc++] = ch;
} else if(ch == ' '){//this is assumed to be one space between words.
word[wc++][wcc] = '\0';//null character
wcc = 0;
}
sentence[n] = ch;
}
word[wc++][wcc] = '\0';
printf("Reversal of sentence:");
finish = n;
for (start = finish - 1; start >= 0; start--) {
if (sentence[start] == ' ') {
for (i = start; i < finish; i++)
putchar(sentence[i]);
finish = start;
}
}
printf("%c\n", terminator);
for(i=wc-1;i>=0;--i){
printf("%s", word[i]);
if(i>0)
putchar(' ');
}
printf("%c\n", terminator);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.