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?
source
share