-1
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma warning(disable:4996)
#define SIZE 100

int main(void){

    char str[SIZE];
    char str2[SIZE];
    int i;
    int len;
    gets(str);

    len = strlen(str);
    for (i = 0; str[i] != NULL; i++) {
        if (str[i] != ' '){
            str2[i] = str[i];
        }
    }
    for (i = 0; i < len; i++){

        printf("%c", str2[i]);
    }
    return 0;
}

It returns the following error:

screenshot

What is the problem?

I just want to copy some elements in str to str2 without spaces, but when I run, it has got some weird letters.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
이지웅
  • 7
  • 1

2 Answers2

3

You need two index variables

  • one to go through str
  • one to tell where to write next to str2

code:

 len = strlen(str);
 int j;
 for (i=0, j=0 ; str[i] != '\0' ; i++) {
    if (str[i] != ' '){
        str2[j++] = str[i]; // store to str2 and increment j
    }
}

Then store a final \0 to str2 at index j

str2[j] = '\0';

Finally, to print the result, you can do that one shot

printf("%s\n", str2);

instead of printing one char at a time.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • Thank you for helping me! May i ask why i have to use two variables? Why don't i just use 'i' to tell where to go and store elemts? – 이지웅 Nov 22 '17 at 14:18
  • Note that `NULL` is a pointer value, not a string terminator and has been changed here to `'\0'` terminator. – Weather Vane Nov 22 '17 at 14:19
  • @이지웅 because `str2` is *uninitialised* and you are skipping elements which remain so. – Weather Vane Nov 22 '17 at 14:28
  • @이지웅 Take a piece of paper and write "how are you", you write "how" to str2, then the space, you skip it, but what is at `str2[3]`? Garbage because you skipped that character in `str2` too. Using `j` allows you to track where is the next non-space character supposed to be written in `str2`. – Déjà vu Nov 22 '17 at 14:30
3

For starters these headers

#include <stdlib.h>
#include <windows.h>

can be removed because neither declaration from the headers is used in the program.

The function gets is unsafe and is not supported by the C Standard any more. Instead use standard C function fgets.

When str is copied in str2 you have to use separate index to access characters in str2 because some characters from str are not copied. Otherwise the array str2 will contain gaps. As result you can not use the previous value of the variable len with the array str2.

Also it is desirable not to copy any other white space characters.

The program can look the following way

#include <stdio.h>
#include <ctype.h>

#define SIZE    100

int main(void)
{
    char str[SIZE];
    char str2[SIZE];

    fgets( str, sizeof( str ), stdin );

    const char *p = str;
    char *p2 = str2;

    do
    {
        if ( !isspace( ( unsigned char )*p ) )
        {
            *p2++ = *p;
        }
    } while ( *p++ );

    for ( p2 = str2; *p2; ++p2 )
    {
        printf( "%c", *p2 );
    }

    return 0;
}

Its output might be

Hello   World
HelloWorld

If you do not study yet pointers then the program can look like

#include <stdio.h>
#include <ctype.h>

#define SIZE    100

int main(void)
{
    char str[SIZE];
    char str2[SIZE];

    fgets( str, sizeof( str ), stdin );

    size_t i = 0, j = 0;

    do
    {
        if ( !isspace( ( unsigned char )str[i] ) )
        {
            str2[j++] = str[i];
        }
    } while ( str[i++] );

    for ( j = 0; str2[j]; ++j )
    {
        printf( "%c", str2[j] );
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks! but it is little bit complicate for me to understand.. i m not good at that 'pointer' things.. is better to get practice on using pointer? – 이지웅 Nov 22 '17 at 14:58