Click here to Skip to main content
15,888,060 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
#include<stdio.h>
#include<string.h>

int xstrlen(char * a);
int main() { 
  char s1[]="bamboo copter";
  char s2[]="doraemon";
  int l1,l2;

 l1= xstrlen(s1);
 l2= xstrlen(s2);
 printf("length of %s is %d\n",s1,l1);
 printf("length of %s is %d\n",s2,l2);
  return 0 ;
}
int xstrlen(char * a){
int length ;
for (a =0;a!='\0';a++){
  length ++;
  
}
}


What I have tried:

C
#include<stdio.h>
#include<string.h>

int xstrlen(char * a);
int main() { 
  char s1[]="bamboocopter";
  char s2[]="doraemon";
  int l1,l2;

 l1= xstrlen(s1);
 l2= xstrlen(s2);
 printf("length of %s is %d\n",s1,l1);
 printf("length of %s is %d\n",s2,l2);
  return 0 ;
}
int xstrlen(char * a){
int length ;
for (a =0;a!='\0';a++){
  length ++;
  //printf("length %d\n",length);
}
}
Posted
Updated 5-Oct-20 20:11pm
v3
Comments
Dave Kreskowiak 5-Oct-20 23:34pm    
And you had a question or a problem ... ?

You should always compile with all warnings enabled (e.g. -Wall with GCC).
Try
C
#include<stdio.h>

int xstrlen(const char * a);
int main()
{
  char s1[]="bamboo copter";
  char s2[]="doraemon";
  int l1,l2;

  l1  = xstrlen(s1);
  l2  = xstrlen(s2);

  printf("length of %s is %d\n",s1,l1);
  printf("length of %s is %d\n",s2,l2);

  return 0;
}

int xstrlen(const char * a)
{
  int length =0;

  for (length = 0; *a != '\0'; ++a)
  {
    ++length;
  }

  return length;
}
 
Share this answer
 
Do you have a question?

In case you are asking why your function does not work, it has two issues. It does not initialize the length and it does not return the length.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900