简体   繁体   English

C语言中的字符串中的反向单词

[英]Reverse Words in a String in C

So basically in this exercise I'm supposed to reverse every word in a given string. 因此,基本上,在本练习中,我应该反转给定字符串中的每个单词。 A word is considered to contain alphanumeric characters and any non-alphanumeric characters will end the word. 单词被认为包含字母数字字符,任何非字母数字字符都将以单词结尾。 For instance "This is an example string, whatever." 例如“无论如何,这都是示例字符串”。 and in reverse "sihT si na elpmaxe gnirts, revetahw.". 反过来说,“就是这样”。

I'm having really hard figuring out what is wrong with my written code. 我真的很难弄清楚我编写的代码出了什么问题。 The code seems to be working, but when I send it to the test server it gives me a Valgrind error. 该代码似乎可以正常工作,但是当我将其发送到测试服务器时,它给出了Valgrind错误。

Here's my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void reversed_words(char *str)
{
      char word[100];
      char *temp;
      char *zet;
      temp=str;
      int k=0;
      while(*temp)  //processing complete string
      {
          if(isalnum(*temp))
          {
             while(isalnum(*temp))      //extracting word from string
             {
                word[k]=*temp;
                k++;
                temp++;
             }
             word[k]='\0';
             k=0;
             strrev(word);   // Reverses the string
             zet=word;       
             while (*zet)   // Copying the reversed word into original string
             {
                *str = *zet;
                zet++;
                str++;
             }
             while (!isalnum(*str)) // Skipping all non-alphanumeric character(s)
             {
                str++;
             }
          }
          temp++;
    }
}

So I'm getting these kinds of error messages: "Use of uninitialised value of size 8" and "Uninitialised value was created by a stack allocation". 因此,我收到以下错误消息:“使用大小为8的未初始化值”和“未初始化值是由堆栈分配创建的”。 I have no idea where the errors are from, I hope someone could help me out with this. 我不知道错误来自哪里,我希望有人可以帮助我解决这个问题。 Thanks in advance. 提前致谢。

Here is a change to the function that makes the minimum changes to fix the bugs in the original code. 这是对函数的更改,它进行了最少的更改以修复原始代码中的错误。 Specifically made two changes: 1) Replaced the last while loop with a simple assignment to str at the bottom of the while (modified from earlier post). 具体进行了两项更改:1)在while的底部用一个简单的str替换了最后一个while循环(从先前的文章进行了修改)。 The right answer has already been calculated and this assignment moves the string forward without another while loop that had issues with falling off the end. 正确的答案已经被计算出来,并且该分配使字符串向前移动,而没有另一个while循环,该循环可能会导致结尾下降。 2) Put an else around the temp++ since it should not be done when a word was found or you can go off the end. 2)在temp++周围放置一个else,因为当发现一个单词时不应该这样做,否则您可以结束。

void reversed_words(char *str)
{
      char word[100];
      char *temp;
      char *zet;
      temp=str;
      int k=0;
      while(*temp)  //processing complete string
      {
          if(isalnum(*temp))
          {
             while(isalnum(*temp))      //extracting word from string
             {
                word[k]=*temp;
                k++;
                temp++;
             }
             word[k]='\0';
             k=0;
             strrev(word);   // Reverses the string
             zet=word;       
             while (*zet)   // Copying the reversed word into original string
             {
                *str = *zet;
                zet++;
                str++;
             }
          }
          else
          {
             temp++;
          }
          str = temp;
    }
}

This might be a problem: 这可能是一个问题:

         zet = word;
         while (*zet)   // Copying the reversed word into original string
         {
            *str = *zet;
            zet++;
            str++;
         }
         while (!isalnum(*str)) // Skipping all non-alphanumeric character(s)
         {
            str++;
         }

When it is processing the last word, it comes out of that first while loop with str pointing to either non-alphabetic junk at the end of the input string, or the '\\0' terminator at the end of the string. 当处理最后一个单词时,它从第一个while循环中出来,其中str指向输入字符串末尾的非字母垃圾或字符串末尾的'\\0'终止符。 In either case, the terminator is not alphanumeric, so the second while loop will continue reading until it finds a byte that happens to be alphanumeric, which is probably in uninitialized memory. 无论哪种情况,终止符都不是字母数字,因此第二个while循环将继续读取,直到找到恰好是字母数字的字节为止,该字节可能在未初始化的内存中。

You could also have problems in your strrev or main functions, but you didn't post them. 您的strrevmain功能中也可能有问题,但没有发布它们。

It's also probably a good idea to consider what will happen if you pass this function a string with a 'word' longer than 99 characters. 考虑如果将“单词”长于99个字符的字符串传递给该函数,将会发生什么,这也是一个好主意。

The "Use of uninitialised value of size 8" probably comes from the fact that you didn't immediately initialise the char pointers (temp and zet), which on a 64bit system would indeed have a size of 8 (bytes). “使用大小为8的未初始化值”的原因可能是您没有立即初始化char指针(temp和zet),而在64位系统上,char指针的大小实际上为8(字节)。 If you don't have a value to initalise a pointer with, you should always make sure to initialise it with 0, so that you know it doesn't point to anything. 如果没有用于初始化指针的值,则应始终确保将其初始化为0,以使您知道它不指向任何内容。

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

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