String length
In this example, we will understand to perform string operations like string length and know how to write, compile and debug it in C language and also learn how to implement it in c.
In order to do this you will need
- Basic knowledge of c programming language.
- As well as know how to operate codeblocks.
C does not support strings as a data type.It allows us to represent strings as character arrays.A string variable is any valid C variable name and is always declared as an array of characters.
The general form of declaration of string variable:
char string_name[size];
strlen() functions:
Description:
This function counts and returns the number of characters in a string.
Syntax:
strlen(string1);
So let's begin with our program
1.Open a codeblocks software.
2.Open new empty file.
3.Copy and paste the code from below.
Input code-
//Write a program to perform string operations like string length.
#include<stdio.h>
#include <string.h>
int main()
{
char a[ 20 ]= "Program";
char b[ 20 ]={'P' ,'r','o' ,'g' ,'r','a','m', '\0'};
char c[ 20 ];
printf( "Enter string:");
gets(c);
printf( "Length of string a=%d\n" ,strlen(a)) ;
printf( "Length of string b=%d\n" ,strlen(b));
printf( "Length of string c=%d\n" ,strlen(c));
return 0;
}
Now simply build the code. Look for any error. Now run the code.
For output enter any number or character series you want and press enter key.
Click the following button to download a program for String Length
Thats it.Thank you for scrolling.
Tags:
string handling function