I am trying to find Length of a string w/o using library function.
char card[16]; //card number in char array.
unsigned int cardno[16]={0}; //card number in int array for calculations.
int i,length=0,len;
printf("Credit Card Number[without any spaces]: ");
gets(card);
for(i=0;card[i]!='\0';i++);
len=i;
length=strlen(card);
printf("%d %d",len,length);
But if i enter a 16 digit card number the output is 16 17 but otherwise[card number less than 16 digit] both output is same. Is this effect of pre and post increment or some other thing. Please explain.
First, you need a longer char array for a 16 digit number:
Then try this:
Do not rely on the value of
i
outside the loop.