简体   繁体   English

用C反转字符串中的单词顺序(代码错误)

[英]Reversing word order in a string with C (error in code)

Could someone guide me in the right direction with this ? 有人可以以此指导我正确的方向吗?

This is a school assignment which i just cant seem to find the error in. For the input 这是学校作业,我似乎无法在其中找到错误。对于输入

"a b c d e f" 

i get 我得到

"f e d c b a" and that is right.

But for 但对于

"1234567890 abcdefghij" i get 

"�' abcdefghij 1234567890"

Can someone guide me in the right direction? 有人可以指引我正确的方向吗? Are the pointers all wrong ? 指针都错了吗?

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

int main()
{  
  char *words [100];
  char *word[11];
  char *str;
  int res, i;
  i = 0;
  res = scanf("%10s",word);
  if (res != 1) return 0;

  while ( res != EOF) {
    str = malloc(strlen(word)+1);
    strcpy(str,word);
    words[i] = str;
    res = scanf("%10s",word);
    i = i+1;
  }

  while (i>=0) {
    printf("%s ",words[i]);
    i--;
  }

  return 0;
}

i starts out 1 element past the end of the array. i从数组末尾开始1个元素。 It's odd that the first example seems to work. 奇怪的是第一个例子似乎起作用。 Not surprising that the second does not. 毫不奇怪,第二个没有。

Try: 尝试:

while (--i >= 0)
{
  printf("%s ", words[i]);
}

and word should be char word[11] , not char *word[11] . 并且word应该是char word[11] ,而不是char *word[11]

The problem is that i is one digit higher than the last filled index of your word so it is may potentially grab garbage. 问题是i比您的单词的最后一个填充索引高一位,因此它可能会带来垃圾。

instead of : i = i+1; 而不是: i = i+1;

use: 采用:

if ( res != EOF) i = i+1;

why don't you use the strrev(str) function to reverse str ? 为什么不使用strrev(str)函数反转str i think that one is an easy way out 我认为这是一个简单的出路

Your problem is int the print loop . 您的问题是int打印loop

while (i>=0) {
    printf("%s ",words[i]);
    i--;
}

when you start this loop the value of i will give you an uninitialized element from the words array, change it this way 当您开始此循环时, i的值将为words数组提供一个未初始化的元素,以这种方式进行更改

while (i > 0) {
    printf("%s ", words[--i]);
}

note : you should also free the pointers, so don't overwrite the words count, if it's only for ilustrative purpose, then it's ok, but even though you should do this 注意 :您还应该free指针,所以不要覆盖单词count,如果仅出于说明目的,那还可以,但是即使您应该这样做

while (i > 0) {
    char *currentWord = words[--i];
    printf("%s ", currentWord);
    free(currentWord);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*function splits a string into an array of words, returns the array and its actual dimension*/
/*------------------------------------------------------------------------------------------*/
char **StringToWords (char *buf, int &dim, int NW, int LW)
{
    char **words = (char**) malloc (NW*sizeof(char*));  
    int i = 0;
    for (; i<NW; i++)
        words[i] = (char*)malloc (LW*sizeof(char));     

    i = -1;
    char *delims = " ,.-";
    char *pch = strtok (buf, delims);
    while (pch != NULL && i<NW)
    {
        strcpy(words[++i], pch);
        pch = strtok (NULL, " ,.-");
    }
    dim = i+1;
    return words;
}

void DestroyWords (char **words, int NW)
{
    for (int i = 0; i<NW; i++)
        free (words[i]);
    free (words);
}

/*--------------------------------------------------------------------------------------------*/
int main()
{
    int NW = 100;   /*maximum number of words*/
    int LW = 50;    /*maximum length of a word*/

    char buf[BUFSIZ];
    printf("Your string:\n");
    gets(buf);

    int dim = 0;
    char **words = StringToWords (buf, dim, NW, LW);
    printf("Reversed string:\n");

    int i = dim;
    while (--i >-1)
        printf("%s ", words[i]);

    DestroyWords (words, NW);
    getchar();
    return 0;
}
/*--------------------------------------------------------------------------------------------*/

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

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