To find the length of specified string
The below program will give u an output of the given string length.The number of words in the given string are counted and the count is displayed that is nothing but the length of the string. This can be explained clearly with the help of an example. The example is given below,
Example:
INPUT: programming
OUTPUT: The length of the string is 11
Here the number of words in the given string are 11 so the length will be 11.
Example:
INPUT: InterviewPrep
OUTPUT: The length of string is 13
Here the number of words in the given string are 11 so the length will be 11.
Example:
INPUT: InterviewPrep
OUTPUT: The length of string is 13
Here the number of words in the given string are 13, so the length will be 13.
Program
| /* C program to find the Length of specified string */ |
#include<stdio.h> //header file
#include<conio.h> // header file
#include<string.h> //header file
int main()
{
char s[30];
int i;
clrscr(); // clears the screen
printf("\n Enter a string: ");
gets(s); //takes the input
i = strlen(s); //finds the length
printf("\n The length of the string is: %d", i);
return 0;
}
OUTPUT:
Enter a string: GoodLuck The length of the string is: 8 |
Program explanation
stdio.h, conio.h and string.h are header files which are used for standard input and output. #include is used to include the files. printf() is used to display the given text on the display screen. gets() is used to take the given input (works same as scanf() ). clrscr() is used to c;ear the display screen. strlen() is used to find length of the string, str means string len means length. The length of the string is stored in variable i. return() is used to return the value to the main()
stdio.h, conio.h and string.h are header files which are used for standard input and output. #include is used to include the files. printf() is used to display the given text on the display screen. gets() is used to take the given input (works same as scanf() ). clrscr() is used to c;ear the display screen. strlen() is used to find length of the string, str means string len means length. The length of the string is stored in variable i. return() is used to return the value to the main()
No comments:
Post a Comment