Answer to Question #33848 in C++ for amit kumar

Question #33848
how can i check that a string contains integer or not in c language ? please write the full program
1
Expert's answer
2013-08-13T09:59:07-0400
#include <stdio.h>
#include <iostream>
#include <string>


using namespace std;


bool contains(string &s)
{
int i = 0;
while (s[i] != '')
{
if (s[i] == '1' ||
s[i] == '2' ||
s[i] == '3' ||
s[i] == '4' ||
s[i] == '5' ||
s[i] == '6' ||
s[i] == '7' ||
s[i] == '8' ||
s[i] == '9' ||
s[i] == '0')
++i;
else return false;
}
return true;
}


int main()
{
cout << "Enter string: ";
string str;
cin >> str;
cout << ((contains(str) == true) ? "Your string is an integer" : "Your string is not an integer");
cin.get();
cin.get();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS