C++ Basic Data types | Practice | GeeksforGeeks

Given a String S. Find out which of the following basic C++ data types it represents and it's size (in bytes).
The possible data types are:
1. Integer
2. Float
3. Double
4. Character
Example 1:
Input:
S=a
output:
1
Explanation:
The string clearly represents char 
and thus the size of char is displayed.
Example 2:
Input
S=98.45685456
Output: 
8
Explanation:
The string represents Double.


Your Task:  
You don't need to read input or print anything. Your task is to complete the function BasicDataType() which takes String S as input parameter and displays the size (in bytes) of the datatype it represents. 

Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)

Constraints:
1<=|S|<=10
0
arajkakabashi1 week ago
tung
0
abhijeetkumarsingh2972 weeks ago
Why the compiler is expecting output ‘4’ for the input ‘7.9’? 7.9 is a double!
0
kaloyankostovbg1 month ago
class Solution {
  public:
    int BasicDataType(std::string s)
    {
        if (!isdigit(s[0]) && s.size() == 1)
        {
            return 1; // char
        }
        if (s.find('.') == -1)
        {
            return 4; // int
        }
        while (s[0] == '0')
        {
            s=s.substr(1);
        }
        
        auto result = double();
        auto i = std::istringstream(s);
        i >> result;
        if ((result < 1.5E-45 || result > 3.4E+38) || s.substr(s.find('.')).size() > 6)
            return 8; // double
        else
            return 4; // float
    }
};
-1
rahulsingh110582 months ago
class Solution { public:   int BasicDataType(string s) {       // code here       int len=s.length(),datasize;       int i=0,temp=0;       if((s[i]>='0' && s[i]<='9')|| s[i]=='.')           {               datasize=4;               for(i=0;i<len;i++)               {                   if(s[i]=='.')                   {                       datasize=4;                       temp=len-(i+1);
                       break;                   }               }                if(temp>=6)               {                   datasize=8;               }           }       else       {           datasize=1;       }       return datasize;         }};
0
saurabhkumarmahra2 months ago
#include<iostream>
using namespace std;
int main()
{
   char a;
   cout<<"The size of the variable is "<<sizeof(a)<<endl;
 }
-1
mdsaif04052 months ago
C++
    int BasicDataType(string s) {
        int len = s.length(), size;        
        if (len == 1) {            
            if (isdigit(s[0]))                
                size = 4;            
            else                
                size = 1;        
        } 
        else 
        {            
            int flag = 0;            
            for (int i = 0; i < len; i++) 
            {                
                if (s[i] == '.') {                   
                    flag = 1;                   
                    int j = len - i + 1;                  
                    if (j > 7)                      
                        size = 8;                   
                    else                       
                        size = 4;              
                }          
            }           
            if (!flag) 
                size = 4;       
        }       
        return size;   
    }
0
2017chb10404 months ago
 if (s.size() == 1)       {           if ((int (s[0]) - '0')<= 9)           {               /* code */               return 4;           }           else           {               return 1;           }       }       else       {              for (int i = 0; i < s.size(); ++i)           {               if (s[i] == '.')               {                   if ((s.size() - (i + 1)) >= 6)                   {                       /* code */                       //    k= "double";                       return 8;                                         }                      else                   {                       //    k= "float";                       return 4;                                          }               }               else if (i == (s.length() - 1))               {                   return 4;               }                  else               {                   continue;               }                                  }                         }
-5
dakshagrawaldlp4 months ago
#include<iostream>
using namespace std;

int main(){
    double s;

    s=98.45685456;
    cout<<sizeof(s)<<endl;
    return 0;
}
0
dakshagrawaldlp4 months ago
#include<iostream>
using namespace std;

int main(){
    char a,s;
    s=a;
    cout<< "Size of s: " <<sizeof(s)<<endl;
    return 0;
}
+2
dkaman794 months ago
class Solution {
  public:
    int BasicDataType(string S) {
        int l = S.length(), ans;
        if (l == 1) {
            //checking if S is integer of size 1
            if (isdigit(S[0]))
                ans = 4;
            else
                ans = 1;
        } else {
            int flag = 0;
            for (int i = 0; i < l; i++) {
                //checking whether S is integer or float/double
                if (S[i] == '.') {
                    flag = 1;
                    int j = l - i + 1;
                    //checking whether S is double
                    if (j > 7)
                        ans = 8;
                    else
                        ans = 4;
                }
            }
            if (!flag) ans = 4;
        }
        return ans;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.