0
votes

The problem enlies with printf(stringOut). It prints an empty array. The function halfstring appears to work correctly but the string it builds never makes it to main.

int main(int argc, char *argv[])
{
char stringIn[30] = "There is no cow level.\0";
char stringOut[sizeof(stringIn)];

halfstring(stringIn, stringOut);

    printf(stringOut);    

return 0;

}

halfstring is supposed to take every odd character in a char array and put it into a new char array without using ANY system-defined string functions (i.e. those found in the string.h library including strlen, strcat, strcpy, etc).

void halfstring(char stringIn [], char stringOut [])
{
    int i = 0;
    int modi;

    while(stringIn[i] != '\0')
    {

        if(i % 2 != 0)

        {

            stringOut[i] = stringIn[i];        

        }

        i++;

    }

}
3
The problem is your halfstring() function is totally broken. Step through it by hand or with a debugger.John Zwinck
stringOut[i] = stringIn[i]; --> int k=0;...stringOut[k++] = stringIn[i]; ... stringOut[k] = '\0';BLUEPIXY
Just as BLUEPIXY wrote, currently you are putting your odd indexed characters in only odd indexed places of stringOut. You need to index the two array, stringIn and stringOut separately - so with different variables, like BLUEPIXY wrote.agiro
Side note: you don't need to put '\0' explicitly at the end of the string literal, the compiler does it for you.user4832129
This looks like C. Why did you add a tag for the different language C++? Remove the unrelated tag.too honest for this site

3 Answers

4
votes

Inside the function halfstring you skipped the first and second characters of stringOut which probably are containing null characters when being declared this is the reason why you got nothing.

You can solve that by adding a new separate indice k for stringOut:

void halfstring(char stringIn [], char stringOut [])
{
    int i = 0,k=0; // create a separate indice for stringOut
    int modi;

    while(stringIn[i] != '\0')
    {

        if(i % 2 != 0)

        {

            stringOut[k] = stringIn[i];
            k++; // increment the indice

        }

        i++;

    }
    stringOut[k]='\0';
}
3
votes

1) You don't need to NUL terminate a string literal:

char stringIn[30] = "There is no cow level.\0";
                                           ^^

2) Your second array (stringOut) results in something like:

{'T', garbage, 'e', garbage, 'e', garbage, 'a', garbage, 'e' ... };

You need to count the number of chars stored in the 2nd array:

void halfstring(char stringIn [], char stringOut [])
{
    int i = 0;
    int n = 0;

    while(stringIn[i] != '\0')
    {
        if(i % 2 != 0)
        {
            stringOut[n++] = stringIn[i];        
        }
        i++;
    }
    stringOut[n] = '\0';
}
0
votes

There are several drawbacks in the program.

For starters there is no need to include the terminating zero in the string literal

char stringIn[30] = "There is no cow level.\0";
                                          ^^^^

because string literals already have the terminating zero.

Secondly usually standard string functions return pointer to the first character of the target string. This allows to chain at least two functions in one statement.

The first parameter is usually declares the target string while the second parameter declares the source string.

As the source string is not changed in the function it should be declared with the qualifier const.

And at last within the function there is used incorrect index for the target string and the string is not appended with the terminating zero.

Taking this into account the function can be written as it is shown in the demonstrative program below

#include <stdio.h>

char * halfstring( char s1[], const char s2[] )
{
    char *p = s1;

    while ( *s2 && *++s2 )  *p++ = *s2++;

    *p = *s2;

    return s1;
}

int main(void) 
{
    char s1[30] = "There is no cow level.";
    char s2[sizeof( s1 )];

    puts( halfstring( s2, s1 ) );

    return 0;
}

Its output is

hr sn o ee.