String length#8569

String length (Editorial)

Find the length of the string using the function strlen from the <string.h> library.

Algorithm implementation

Declare the character array.

char s[110];

Read the input string.

gets(s);

Print the input string.

puts(s);

Print the length of the input string.

printf("%d\n",strlen(s));

Algorithm implementation — C++

Read the input string.

getline(cin, s);

Print the input string.

cout << s << endl;

Print the length of the input string.

cout << s.length() << endl;

Algorithm implementation — dynamic array

#include <stdio.h>

char *s;

void puts(char *s)
{
  while(*s)
    printf("%c",*s++);
  printf("\n");
}

int strlen(char *s)
{
  int len = 0;
  while(*s++) len++;
  return len;
}

int main(void)
{ 
  s = new char[110];
  gets(s);
  puts(s);
  printf("%d\n",strlen(s));
  delete[] s;
  return 0;
}

Java implementation

import java.util.*;

public class Main 
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    String s = con.nextLine();
    System.out.println(s);
    System.out.println(s.length());
    con.close();
  }
}

Python implementation

Read the input string.

a = input()

Print the input string.

print(a)

Print the length of the input string.

print(len(a))