简体   繁体   中英

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). 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). 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. example char string[]="word" looks like: |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.

By the way, sentence creation and initialization:

  char sentence[MAX_SENTENCE_LEN] = {' '};  

Does not guarantee contents for the entire length of the char array.
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. So, if your code is not running on your machine, it is likely that you just need to initialize sentence to \\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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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