Standard Input having weird characters with them in different programming lanuage

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:

┌─[[email protected]]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[[email protected]]─[~/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:

┌─[[email protected]]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[[email protected]]─[~/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] != '')
    len++;
   printf("The Entered String is : %sn", s);
   printf("The Length of Entered String is : %dn", len);
   return 0;
}

Output:

─[[email protected]]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[[email protected]]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[[email protected]]─[~/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] != '')
    len++;
   printf("The Entered String is : %sn", s);
   printf("The Length of Entered String is : %dn", len);
   return 0;
}

Output:

┌─[[email protected]]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[[email protected]]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello
The Entered String is : hello

The Length of Entered String is : 6
┌─[[email protected]]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello^[[C
The Entered String is : hello

The 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:

┌─[[email protected]]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[[email protected]]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[[email protected]]─[~/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:

┌─[[email protected]]─[~/Desktop]
└──╼ $./try.sh
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[[email protected]]─[~/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 :

┌─[[email protected]]─[~/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

Answer

how it is becoming 8? and when i print them why are they not showing up as ‘[‘, ‘C’, ‘^’ are printable.

The three chars you see when pressing the right arrow key are combined to form an escape sequence. The first character being ESC.

ESC is not printable but is likely consumed by your terminal which is going into a state where it’s waiting for something more to come. When it comes, it’ll act on it.

0x1b  // ESC
0x5b  // [ - CSI - Control Sequence Introducer
0x43  // C - CUF - Cursor Forward

If you remove the ESC from the output, your terminal will gladly print [C but when preceeded by ESC it forms a command as shown above.

Leave a Reply

Your email address will not be published. Required fields are marked *