0

Hello my function should print str1 beginning with str2 (if found), I would like to return a new string (newStr) in the following function but it doesn't work. Some help please

char *myFunction(char *str1, char *str2){
    int strLen;
    int i;
    int j;
    char temp;
    char *newStr;

    strLen=0;
    while(str1[strLen]!='\0'){
        strLen++;
    }

    i=0;
    j=0;

    while(i<=strLen && str1[i]!='\0'){
        if(str1[i]==str2[j] ){
            newStr[i]=str1[i];
            j++;
        } else {
          newStr[i]=str1[i];
        }
        i++;
    }

    return (newStr);
}
7
  • 1
    newStr is uninitialized which causes UB. You need to use malloc() to dynamically allocate memory. BTW why is newStr[i]=str1[i]; both in the if and else block? Commented Dec 28, 2021 at 15:58
  • Allocate memory for newStr first. Read about malloc. Commented Dec 28, 2021 at 15:59
  • Because if str1="Apples are good for health" and str2="good" I want to display "good for health" Commented Dec 28, 2021 at 16:01
  • Are you prohibited from using built-in functions like strcpy() and strcat()? Commented Dec 28, 2021 at 16:13
  • Yes I can't use native function at all Commented Dec 28, 2021 at 16:13

2 Answers 2

1

char *newStr is uninitialized; you must allocate memory to it, before assigning any value to it.

Allocate memory using malloc or calloc.

Sign up to request clarification or add additional context in comments.

Comments

1

For starters the function should be declared like

char * myFunction( const char *str1, const char *str2 );

because the passed strings are not being changed within the function.

If the function has to return a new string then you need to allocate a character array where the string will be stored. However you are using an uninitialized pointer newStr

char *newStr;

The condition in the while loop

while(i<=strLen && str1[i]!='\0'){

does not make a great sense.

The variable j in fact is not used.

The if-else statement within the while loop does not make a sense.

If you are allowed to use standard C string functions then your function can be implemented very easy.

#include <string.h>

char * myFunction( const char *s1, const char *s2 )
{
    char *p = strstr( s1, s2 );

    if ( p != NULL )
    {
        size_t n = strlen( p );

        s1 = p;

        p = malloc( n + 1 );

        if ( p != NULL ) memcpy( p, s1, n + 1 );
    }

    return p;
} 

Otherwise the function can be defined the following way

char * myFunction( const char *s1, const char *s2 )
{
    size_t n1 = 0;
    while ( s1[n1] ) ++n1;

    size_t n2 = 0;
    while ( s2[n2] ) ++n2;

    char *p = NULL;

    if ( !( n1 < n2 ) )
    {
        int found = 0;
        size_t i = 0;

        while ( !found && i < n1 - n2 + 1 )
        {
            if ( s1[i] == s2[0] )
            {
                size_t j = 1;

                while ( j < n2 && s1[i + j] == s2[j] ) ++j;
                
                found = j == n2;
            }

            if ( !found ) ++i;
        }
    
        if ( found )
        {
            p = malloc( n1 - i + 1 );
            
            if ( p != NULL )
            {
                size_t j = 0;

                do 
                {
                    p[j] = s1[i + j];
                } while ( p[j++] != '\0' );
            }
        }       
    }

    return p;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.