简体   繁体   中英

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.

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". 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). 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. 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.

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. 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.

You could also have problems in your strrev or main functions, but you didn't post them.

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.

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). 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.

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