I am getting confused with the standard input of these programming languages :
[Note:]
I added details about so many programming languages as the problem with all of them is same in this matter and my only focus in this question is how can I overcome this problem and have a true terminal like experience from within the program itself.
Firstly,
Java
Code:
import java.util.Scanner;
public class Try{public static void main(String args[]){Scanner sc = new Scanner(System.in);System.out.println("Enter a String : ");String s = sc.nextLine();System.out.println("The Entered String is : " + s);System.out.println("The Length of Entered String is : " + s.length());sc.close();}
}
Output:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
When I press the arrow keys ^[[C
show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!
But even in this case how it is becoming 8? and when i print them why are they not showing up as '[', 'C', '^' are printable.
Python
Same as Java here!
Code:
s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))
Output:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String :
hello^[[D
The Entered String is : hello
The Length of Entered String is : 8
C
Same thing here too..
Code :
#include <stdio.h>int main()
{char s[20];int len = 0;printf("Enter a String : \n");scanf("%[^\n]%*c", s);while (s[len] != '\0')len++;printf("The Entered String is : %s\n", s);printf("The Length of Entered String is : %d\n", len);return 0;
}
Output:
─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
But with a slightly different code :
Code:
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{char s[MAX_LIMIT];int len = 0;printf("Enter a String : \n");fgets(s, MAX_LIMIT, stdin);while (s[len] != '\0')len++;printf("The Entered String is : %s\n", s);printf("The Length of Entered String is : %d\n", len);return 0;
}
Output:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello
The Entered String is : helloThe Length of Entered String is : 6
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello^[[C
The Entered String is : helloThe Length of Entered String is : 9
Here we cal see clearly that it takes the \n
too as a part of the string so 9
C++
Same here too ..
Code:
#include <iostream>
#include <string>
using namespace std;int main(){string s;cout << "Enter a string : " << endl;cin >> s;cout << "The Entered String is : " << s << endl;cout << "The Length of Entered String is : " << s.length() << endl;return 0;
}
Output:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
Bash
Same here too.. Code:
#!/bin/bash echo "Enter a String : "
read str
echo "The Entered String is : $str"
len=`expr length "$str"`
echo "The Length of Entered String is : $len"
Output:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
Exception
But to all this there is a exception
It is with python.
Instead of running code from a file if we use the python interpreter directly:
Here is the Terminal output :
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = input("Enter a String : \n")
Enter a String :
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
>>> s = input("Enter a String : \n")
Enter a String :
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
Here in spite of pressing the arrow keys or escape or home the output is same.
I looked in whats the string is having thats making it 8 characters long:
['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']
But here too [
and C
is printable !
Can any one explain whats all these about?
And how can i get rid of them?
If you need any more details or clarity please ask