Does not display string, in C

Asked

Viewed 157 times

0

I’m having trouble copying string in C, it doesn’t want to work.

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

int main()
{
    int count;
    char str1[20] = "Hello World", str2[20];
    for(count = 0; str1[count]!='\0'; count++)
        str2[count] = str1[count];
    str2[count] = '\0';
    printf("%c%", str2[count]);
    system("pause");
    return 0;
}

I have tried with %c, and %s, and with keys no for, but nothing appears on the screen, %appears (null), I know there is the option to use strcpy.

2 answers

1


You need to put

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

str2[count] is a character, specifically the '\0' that you just put in.

  • You don’t need the [Count]?

  • str2[count] is a character, specifically the '\0' that you just put in.

1

If showing only one character of the sequence is "printf("%c", str2[n])". But if you want to display all the text of the array is "printf("%s", str2)".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.