Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sagarladla/ca6cad26b9380ce9c55ecf58504e52fc to your computer and use it in GitHub Desktop.
Save sagarladla/ca6cad26b9380ce9c55ecf58504e52fc to your computer and use it in GitHub Desktop.
microsoft internship problem
/*
1) Given a string, you have to check if it is a valid number or not.(The number can be signed, floating point/integer).
If it is a number return true else return false.
The constraints were :-
i) No decision statements allowed (No if-else,no switch-case).
ii) No ternary conditional operators allowed (? : not allowed).
iii) No looping statements allowed (No for/while/do-while).
*/
#include<iostream>
using namespace std;
bool dot=false;
bool endt=false;
bool finalr=true;
bool isdot(char c)
{
return(c=='.'&&(!dot)&&(dot=true));
}
bool validno(char c)
{
return(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9');
}
bool rec(char *s)
{
endt=*s=='\0';
bool thisis=endt||validno(*s)||isdot(*s);
finalr=finalr&&thisis;
bool ttm=(*s=='.')&&(*(s+1)=='\0')&&(finalr=false);
s++;
bool tm=(!endt)&&rec(s);
return(finalr);
}
bool check(char s[])
{
dot=(s[0]=='.');
bool frst=s[0]=='-'||s[0]=='+'||dot||validno(s[0]);
bool tm=(*(s+1)=='\0')&&(*s=='.'||*s=='+'||*s=='-')&&(finalr=false);
return(frst&&rec(&s[1]));
}
int main()
{
dot=false;
endt=false;
finalr=true;
char s[1000];
cin>>s;
cout<<check(s);
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment