Write a C function string length that takes as input a string and returns as out
ID: 3628100 • Letter: W
Question
Write a C function
string length that takes as input a string and returns as output the number of characters
in the string. The input parameter must be of type char*, and you may assume that the
string is terminated by the null character.
Place your string length in a C le that includes a main method containing at least ve
tests of the function. For example, the call string length("hello world") should return
11, and the call string length("5") should return 1. (Of course, you should think of ve
additional tests.)
Explanation / Answer
// working fine ....use proper compiler.
// this is perfect code boss !!
#include<stdio.h>
#include<conio.h>
int string_length(char *s)
{
int n=0;
while(*s++)
n++;
return n;
}
int main()
{
char *s = "Hellow world";
printf("%d" ,string_length(s));
printf("%d" ,string_length("s"));
printf("%d" ,string_length("12"));
printf("%d" ,string_length("54322"));
getch();
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.