Adsense 728 * 15

Search

Custom Search

C Program to Find the Length of a String without using Strlen

Write a c program to find length of a string without using strlen() function.  In this programming questions, we have to write a code to find the length of a string.

We can easily find the length of string using inbuilt strlen() function. Let's first find the length of a string using strlen() function.

C program to compare two strings using strcmp() function

C Program to Find the Length of a String using Strlen


In this programming example, we first take an input string from a user and then we print the length of a string using an inbuilt strlen() function.

#include<stdio.h>

int main() {
 
   char str[100];
  
   //Input string
   printf("Enter a string \n");
   gets(str);
 
   //Print the length of a string
   printf("\n String length is %d", strlen(str));
 
   return 0;
}


How to Find the Length of a String without using Strlen


In C, Every String is terminated with Null Character ('\0'). We can use this concept to traverse a string until null character is encountered.   Once the null character is encountered we terminate the loop and print a count.


C program to check whether two strings are anagrams of each other

How to Read a String with Spaces using Scanf


C Program to Find the Length of a String without using Strlen


C Program to Find the Length of a String without using Strlen



In the above example, we have written a code to print the length of a string using strlen(). In this example, we print the length of a string using while loop.


#include<stdio.h>

int main() {
 
  char str[100];
  int count=0;
 
  printf("Enter a string \n");
  gets(str);
 
  /* Run a loop until null('\0')
    character is encountered. */

  while(str[count]!='\0'){
         
    /* Increment count. */
    count++;
  }
 
  printf("\n String length is %d", count);
 
  return 0;
}


Programming questions on strings

Find Length of String using For Loop


#include<stdio.h>

int main() {
 
 char str[100];
 int i;
 
 printf("Enter string \n");
 gets(str);
 
 for(i=0;str[i]!='\0';i++);
 
 printf("\n String length is %d", i);
 
 return 0;
}

Output:

Enter a string    cprogrammingcode

String length is 16

Program to reverse a string using stack

C program to check whether a character is vowel or consonant

No comments:

Post a Comment