Tuesday, April 17, 2012

How to traerse/print character by character in a string without using strlen() ?

How to traerse/print character by character in a string without using strlen() ?


Usually we will do like this:

char szString[100]={0};
scanf("%s",szString);

for(int i = 0; i <strlen(szString); i++);
{
printf("%c",szString[i];
}


without knowing strlen(), we can print the same thing as below:

char szString[100]={0};
scanf("%s",szString);

for(int i = 0; szString[i]; i++);
{
printf("%c",szString[i];
}

if szString reaches NULL, it will returns zero, so for loop will be
terminated for that case.

No comments: