Programming Examples

Cpp program to input name and find the length of the stored string


Write a program to input name and find the length of the stored string

Solution

#include <iostream>
using namespace std;
int main()
{
    char name[20];
    int a;
    cout << "Enter your name : ";
    cin.getline(name,20);
    int len = 0;
    for (a = 0; name[a] != '\0'; a++)
    {
        len++;
    }
    cout << "Length of the string is: " << len;
    return 0;
}
Output

Enter your name : Infomax Academy

Length of the string is: 15


Note: gets() function is an alternative to getline(), but it is prone to buffer overflow issues.


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2024 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz