Given a string we have to check whether string contain digits only.
Example:
Input:1234
output:true
Input:abcd1
output:false
Logic:
step 1-find a length of string and initialise count variable.
step 2-iterate whole string and if you found number then increment count.
step 3-if count==length then return true
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; int count=0; for(int i=0;i<s.length();i++) { if(isdigit(s[i])) { count++; } } if(count==s.length()) { cout<<"string contain digit only"<<endl; } else { cout<<"string contain not digit only"<<endl; } }
Output:
1234 string contain only digit
Time Complexity-O(N)
Space Complexity-O(1)