Object

Write a program to read a string and print its length.

Algorithm

  1. Declare a string str up to 80 characters.
  2. Read string str.
  3. Write length of string str, using strlen(str).
  4. Exit.

Flowchart

C++ Source Code

// program 93
#include<iostream>
#include <cstring>
using namespace std;

int main()
{
  char str[80];
  cout << "Enter the string: ";
  cin.getline(str, 80);

  cout << "\n";
  cout << "'" << str << "' is " << strlen(str) << " characters long.";

  return 0;
}

C Source Code

/*program 93*/
#include<stdio.h>
#include<string.h>

int main()
{
  char str[80];
  printf("Enter the string: ");
  gets(str);
  printf("\n\n");
  printf("'%s' is %d characters long.\n", str, strlen(str));
  return 0;
}

Output

Enter the string: Pakistan is my country.

‘Pakistan is my country.’ is 23 characters long.

Design a site like this with WordPress.com
Get started