strlen.c
K&R, p. 39
download
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
int strLen(char[]); // strlen() is declared in string.h, included by stdio.h
int main()
{
char word[LENGTH];
printf("Type a word: ");
scanf("%s", word);
printf("Length of \"%s\" is %d\n", word, strLen(word));
return 0;
}
int strLen(char s[]) // returns the length of string s[]
{
int i = 0;
while (s[i] != '\0')
{
i++;
}
return i;
}
/*
gcc strlen.c -o strlen
./strlen
Type a word: abracadabra
Length of "abracadabra" is 11
*/
Comments
Post a Comment