Sunday, September 7, 2014

determine the number of characters in a string


Problem

write a program in c language that passes array index as an argument to a user defined function.The function then counts the number of characters in that string and displays it to the user.

Note: use pointers to count the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int mystrlen(char *p);
main()
{
 int count;
 char p[]="Honesty is the best policy.";
 count = mystrlen(p);
 printf("\n\nNumber of characters in the string = %d\n\n",count);
 system("pause");
}
int mystrlen(char *p)
{
 printf ("\n");
 int count = 0;
 while (*p != '\0' )
 {
  printf ("%c",*p);
  p++;
  count++;
 }
 return (count);
}

Sample Output


number of characters in a string

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact