You are not logged in. Please login at www.codechef.com to post your questions!

×

need help in the string length function

here is my code to find out the length of the string.

include <stdio.h>

include <stdlib.h>

int main()
{
char st[ ]="hithere";
printf("\a");
int i;
i=len(st);
printf("%s",st);
printf("\a%d",i);
return 0;
}
int len(char s)
{
int l=0;
while(
s != '\0');
{

 l++;        
 s++;

}

return (l);
}

cant figure out the problem please help. (making in codeblocks)

asked 14 Jun '18, 22:22

srij_kh's gravatar image

0★srij_kh
11
accept rate: 0%


In your len function, you were accessing a character not a pointer. so s++ is next character alphabetically not next character of the string. if your parameter is accepting a pointer then s++ would be next element of string or character array

include <stdio.h>
include <stdlib.h>
int main() 
{ 
 char st[ ]="hithere"; 
 printf("\a"); 
 int i; 
 i=len(st); 
 printf("%s",st); 
 printf("\a%d",i); 
 return 0; 
} 
int len(char s[]) // or(char *s) 
{ 
int l=0; 
while(s != '\0'); 
{
 l++;        
 s++;
}
return (l); 
}
link

answered 14 Jun '18, 23:53

ay2306's gravatar image

3★ay2306
2329
accept rate: 11%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,477
×644

question asked: 14 Jun '18, 22:22

question was seen: 103 times

last updated: 14 Jun '18, 23:53