Given a string. Print the string and its length.
Input. One string with no more than 100 characters.
Output. On the first line print the input string. On the
second line print its length.
Sample input 1 |
Sample output 1 |
Programming Principles 1 |
Programming Principles 1 24 |
|
|
Sample input 2 |
Sample output 2 |
This is a cat. |
This is a cat. 14 |
strings
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))