C++

Objectives : Advocating for string-object of string class as a better candidate to get user's input, than using a character-array of fixed length . In this example, we will use string-object to get a line of an input-stream, fetch a character array from a string , and use "ctype.h" library of C language to filter space, alphabets and digits from the character array, which created at the previous step.

  • File Name : string_isdigit_isalpha4.cpp
  • Functions from C language: isalpha(), isdigit() and isspace();
  • Using : getline(cin,str1);
    • Syntax istream getline(isstream istr, string& str1)
  • Code : string_isdigit_isalpha4.txt
     
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views:

Step: 4 Brief Discussion:

Given to choose between a character-array or string object to get user's input, the later, string object would definitely be more dynamic or simpler choice, over the character array. The character arrays are predefined spaces, therefore when we use it as receiver, it won't stretch-out to accept the input which is more than it's capacity. We can manipulate, the end point of a string object is set with a delimiter or termination signal, like return or custom character; therefore string will return the length of the actual length of characters to process.

The illustration below, shows that the size of an  character-array, was 100 unit before and after adding characters to it. The string object returned the actual number of characters received as an input from the user.

However, to get to the individual characters, we have to treat the string-object as an array.

Special Notes : isotream and fstream

    • #include <iostream>
      #include <fstream>