To make a string a null string i wrote this:
#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 this 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 runs all right while when in the second example it prints the first letter of the string (in this example F). Why is this?
C strings are null-terminated. As long as you only use the functions assuming null-terminated strings, you could just zero the first character.