ch2-strlen (String length)

Chapter_2     Exercise_2-1     computed enum     Exercise_2-2







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
*/









Chapter_2     Exercise_2-1     computed BACK_TO_TOP enum     Exercise_2-2



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo