Write a program to find string length in c

20 Sep 2022 Balmiki Mandal 0 C Programming

Program to Find String Length in C

In C programming, a string is a sequence of characters that is terminated by a null character \0. The length of a string is the number of characters in the string, excluding the null character.

The length of a string is an essential concept in C programming because it is used in many standard library functions that operate on strings. For example, the strlen function is a standard library function in C that returns the length of a string. The strcpy function copies a string from one location to another, and the strcat function concatenates two strings.

Example of a string finding String length in C:

 

This program uses a while loop to count the number of characters in the string str. The loop keeps iterating until it reaches the null character \0, which marks the end of the string. The length variable is incremented each time the loop iterates, so it contains the length of the string when the loop is finished.

C programming

#include
int main()
{
    char s[10];
    int i;
    printf("Enter the string:");
    scanf("%s",s);
    printf("string is: %d\n",s);
    for(i=0;s[i];i++);
    printf("String length is:%d\n",i);
}

Output:

Enter the string: ELECTRO4U
string is: ELECTRO4U
String length is:9

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

Design a function to find string length in c

Design a Function to print the string character by character

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.