Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Wednesday, May 28, 2014

String length

This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h.

C program to find string length

#include <stdio.h>
#include <string.h>
void main()
{
   char a[100];
   int length;
   clrscr();
   printf("Enter a string to calculate it's length\n");
   gets(a);
   length = strlen(a);
   printf("Length of entered string is = %d\n",length);
   getch();
}

C program to find string length without strlen

#include <stdio.h>
#include<conio.h> 
void main()
{
   char array[100], *pointer;
   int length = 0;
   clrscr();
   printf("Enter a string\n");
   gets(array);
   pointer = array;
   while(*(pointer+length))
   length++;
   printf("Length of entered string = %d\n",length);
   getch();
}

Function to find string length

int string_length(char *s)
{
   int c = 0;
   while(*(s+c))
   c++;
   return c;
}

No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC