简体   繁体   中英

Unexpected output of strlen function

I was trying to implement a function that will modify a string:

The code is as follows:

test.h file :

#ifndef header_file
#define header_file

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

char * findWord(char *, char *);

#endif

test.c file:

#include"test.h"

char * findWord(char *array, char *action)
{
    char testchar;
    int count=0, i=0, j, start = 0, end, wordLength = 0, charCount =0, k=0;
    char *temp = malloc(sizeof(char)*400);
    char *word = malloc(sizeof(char)*30);
    char *replaceString = malloc(sizeof(char)*80);

    if(strcmp(action,"replace") == 0)
    {
        while((testchar = array[i]) != '\0')
                {

            if(testchar == ',')
            {
                start = i+1;
                i++;
                continue;
            }
            else if(testchar == ':')
            {
                 end = i;
                 word[charCount] = '\0';
                 charCount = 0;
                 printf("Start is: %d \n", start);

                 for(j=0; j< strlen(array); j++)
                 {
                    if(j == start)
                    {
                        sprintf(replaceString, "%s%s%s", "replace_",word,"_ii");
                        printf("Replace String for word %s is %s.\n",word,replaceString);
                        strcat(temp,replaceString);
                        j = (j-1)+(strlen(word));
                        k= strlen(replaceString);

                        printf("The value of J is %d for word %s.\n",j,word);
                    }
                    else
                    {
                        temp[k++] = array[j];
                    }
                 }
                 temp[k] = '\0';
                 k=0;

                printf(" Words %s is replaced. The new string is:\n", word);
                printf("%s\n",temp);
                memset(word,'0',30);
                memset(temp,'0',400);
                memset(replaceString,'0',80);
                i++;
                continue;
            }

            if(testchar != 'Y')
            {
                word[charCount] = testchar;
                charCount++;
            }

            i++;
        }
    }

    else if(strcmp(action,"MISSING") == 0)
    {

    }
    else if(strcmp(action,"EMPTY") == 0)
    {

    }
    else
        printf("Something went wrong.\n");

   free(temp);
   free(word);
   free(replaceString);
}

main.c file:

#include"test.h"

int main()
{
    char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
    findWord(sc, "replace");

    return 0;
}

The expected output is:

Replace String for word cn is replace_cn_ii.
replace_cn_ii:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y

. . . 10 output.

But It is giving the garbage value due to unexpected behavior of strlen() . The value of word after strcat() function is changed automatically.
Where am I going wrong? Let me know the issue and how to fix it.

Thanks!

You're calling strcat() on temp , but temp does not contain a valid string. This gives undefined behavior.

You must make sure temp is valid first, ie make it an empty string:

*temp = '\0';

Of course you must also make sure the allocation has succeeeded.

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