Make string zero on one line

To make a string a null string, I wrote the following:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[15]="fahad uddin";
    strlen(str);
    puts(str);
    for(int i=0;str[i]!='\0';i++)
        strcpy(&str[i],"\0") ;
    puts(str);
    getch();
    return 0;
}

Before that I tried:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[15]="fahad uddin";
    strlen(str);
    puts(str);
    for(int i=0;str[i]!='\0';i++,strcpy(&str[i],"\0"))
        ;
    puts(str);
    getch();
    return 0;
}

In the first example, the program works correctly, and in the second example, it prints the first letter of the string (in this example, F). Why is this?

+5
source share
5 answers
Lines

C end with zero. As long as you only use functions that assume null-terminated strings, you can simply zero out the first character.

str[0] = '\0';
+10
source
memset(str,0,strlen(str)); /* should also work */
memset(str,0,sizeof str); /* initialize the entire content */
+7
source

for(int i=0;str[i]!='\0';i++,strcpy(&str[i],"\0")); - ++ , strcpy, str [1] - str [0] - , .

, KennyTM - , , /.

+5

i++,strcpy(&str[i],"\0") i++, strcpy(), i . , .

, , .

KennyTM '\0' str[0] = '\0';, , , .

memset(), , , .

, strcpy() , str[] .

+3

:

bzero(string_name, size_of_string);

Also include the <string.h>lib file . I think this should work.

-2
source

All Articles