简体   繁体   English

c中的二维数组,反转并存储一个句子

[英]two dimensional array in c, reversing and storing a sentence

Hey I'm really new to programming and having trouble with arrays. 嘿,我真的是新手,对数组有麻烦。 Can someone help me with this project. 有人可以帮我这个项目。 "c programming a modern approach: modify a program that reverses the words of a sentence so that it stores the words in a two dimensional char array as it reads the sentence, with each row of the array storing a single word. assume that the sentence contains no more than 30 words and no word is more than 20 characters long. Be sure to store a null character at the end of each word so that it can be treated as a string" (also i don't get what its saying about the null character). “ c用一种现代方法编程:修改一个程序以使句子中的单词反向,以便在读取句子时将其存储在二维char数组中,数组的每一行都存储一个单词。假定该句子包含不超过30个单词,且每个单词的长度不超过20个字符。请确保在每个单词的末尾存储一个空字符,以便可以将其视为字符串”(同样,我也听不懂它的意思。空字符)。 here's my try but it's not working. 这是我的尝试,但是不起作用。 i think i'm close though. 我想我虽然接近。

#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;}

i wrote a new code which i think is closer to what i want but it still won't run. 我写了一个新代码,我认为它更接近我想要的代码,但仍然无法运行。 do i have a faulty compiler or what? 我的编译器有问题吗? anyway here's the new code 无论如何,这是新的代码

#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; 

    }

Your code worked perfectly in my environment (Windows, C99 compiler, 32bit build). 您的代码在我的环境(Windows,C99编译器,32位版本)中运行完美。 I entered a short sentence, and it reversed it: 我输入了一个简短的句子,然后它反过来了:

在此处输入图片说明

Regarding : i don't get what its saying about the null character 关于我不了解空字符的说法

a C string is defined by a null character: \\0, at the end of a char array. C字符串由char数组末尾的空字符\\ 0定义。 example char string[]="word" looks like: |w|o|r|d|\\0| 示例char string [] =“ word”看起来像:| w | o | r | d | \\ 0 | in memory. 在记忆中。

Without the \\0 , it would simply be a char array, but not a string, and would therefore not be useable in any of the string functions such as strcpy() , strlen() , etc. 没有\\0 ,它将只是一个char数组,而不是一个字符串,因此无法在任何字符串函数(例如strcpy()strlen()strlen()中使用。

By the way, sentence creation and initialization: 顺便说一下, sentence创建和初始化:

  char sentence[MAX_SENTENCE_LEN] = {' '};  

Does not guarantee contents for the entire length of the char array. 不保证char数组整个长度的内容。
This may be the reason your environment is not running your code, while my environment does. 这可能是您的环境没有运行代码而我的环境却在运行代码的原因。 Depending on compiler, OS, and other random factors, sentence could be filled with anything. 根据编译器,操作系统和其他随机因素, sentence可能填充任何内容。 So, if your code is not running on your machine, it is likely that you just need to initialize sentence to \\0 . 因此,如果您的代码未在计算机上运行,​​则可能只需要将sentence初始化为\\0 Replace that line with these: 用以下内容替换该行:

  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)

Also by chance, if the user input results in string length == MAX_SENTENCE_LEN, then your program will crash as there is only enough room in sentence for MAX_SENTENCE_LEN-1 + \\0 . 同样偶然的是,如果用户输入的字符串长度== 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.

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