Adsense 728 * 15

Search

Custom Search

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

Write a c program to find the length of a string without using strlen() function. Using strlen function you can easily calculate the length of string.

String is terminated with Null Character ('\0'). Using this concept we can traverse the string until null character is encountered.   Once the null character is encountered we terminate the loop and show them a count.

Program to Count number of vowels in a string.

Reverse string without using strrev function.

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


#include<stdio.h>

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

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



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 string    cquestions.in

String length is 13

No comments:

Post a Comment