Count the length of strintg without using strlen?
Is there a way to count the characters in a string without using strlen? I was trying to write my own string length function by using the array....Do
I have to initialize the char array? Can someone
just give me a hint please??? Here are part of the
code I have so far, very very ugly.......I don't
even know what am I doing anymore......
#include <stdio.h>
int length(char[]); /* prototypes the function for the length of the char */
char oddeven(int); /* prototypes the function for even or odd char length */
int main(void)
{
int x;
char array[26];
length(array);
x=length(array);
printf("\nThe length of the string is %d characters\n", x);
return 0;
}
int length(char array[]) /* define the function of char length */
{
int i = 0;
/* char array[26];*/ /* 25 characters + 1 for the \0 */
char c;
printf("\nPlease enter a string, maximum of 25 characters;\n");
c = getchar();
while(c != '\0' && c != '\n')
{
array[i++] = c;
return i;
}
}