Detecting if input is a string or an integer - c++

Please tell me what is wrong in my approach.
#include <iostream>
#include <string>
using namespace std;
string datatype(string x) {
for (int k = 0; k < strlen(x.c_str());) {
for (int i = 0; i < 10; i++) {
char z = i;
if (x[k] == z) {
k++;
}
else {
return "string";
}
}
}
return "int";
}
int main() {
string inp;
cin >> inp;
cout << datatype(inp);
}
Whatever i enter, it always returns "string".
I have seen the other questions posted here but please tell me what is wrong in my approach.

The standard library has the isdigit function that tells you if the char is a digit.
Here you check that each char of your input is a digit, as soon as a character that is not a digit is found string is returned, else int.
For example 1234 returns int, sdf returns string.
string datatype(string str) {
for (unsigned char c : str) {
if (!isdigit(c)) {
return "string";
}
}
return "int";
}
Edit:
This solution also handles leading - and +. It will return int for -10 and +10 but returns string for +1+1 or -10+10.
string datatype(string str) {
if (str.size() == 1) {
return isdigit(str[0]) ? "int" : "string";
}
bool isInt = true;
for (int i = 1; i < str.size(); i++) {
isInt = isInt && isdigit(static_cast<unsigned char>(str[i]));
if (!isInt) {
break;
}
}
if (isInt) {
unsigned char c = str[0];
isInt = isInt && (c == '-' || c == '+' || isdigit(c));
}
return isInt ? "int" : "string";
}

First of all Include (cstring) as header since x.c_str is not in iostream and string. Then,
When you are doing char z=i; here you are not storing the character equivalent of i in z but the ascii value of i.
Next, You are returning string at the first mismatch between i and x[k]. You should return string if you cannot find a match with any of the possible 10 digits.
You can have a look at the modified code.
#include <iostream>
#include<cstring>
#include <string>
using namespace std;
string datatype(string x) {
for (int k = 0; k < strlen(x.c_str());k++) {
int flag=0;
for (int i = 0; i < 10; i++) {
// char z = i;
if ((x[k]-'0') == i || (k==0 && x[k]=='-')) {
flag=1;
break;
}
}
if(flag==0)
return "string";
}
return "int";
}
int main() {
string inp;
cin >> inp;
cout << datatype(inp);
}

Related

isPalindrome homework exercise

Write a program that uses the function isPalindrome given in Example 6-6 (Palindrome). Test your program on the following strings:
madam, abba, 22, 67876, 444244, trymeuemyrt
Modify the function isPalindrome of Example 6-6 so that when determining whether a string is a palindrome, cases are ignored, that is, uppercase and lowercase letters are considered the same.
The isPalindrome function from Example 6-6 has been included below for your convenience.
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length – 1 – i]) {
return false;
} // if
} // for loop
return true;
}// isPalindrome
Your program should print a message indicating if a string is a palindrome:
madam is a palindrome
My program so far is this
#include <iostream>
#include <string.h>
using namespace std;
int main () {
bool isPalindrome (string str);
string str;
int length = str.length();
cout << "Enter a string: ";
getline (cin,str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << "Is not a Palindrome";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
}
I do not know what im doing wrong I sent everything to make sure it matches the word backwards and then when it is true it will return true. I am very to new to programming and I am sorry if my code is a little sloppy.
This is a modification of your code. It wasn't too logical that you were declaring the function inside so i just put it outside.
#include <iostream>
#include <string.h>
using namespace std;
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << "Is not a Palindrome";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
return false;
}
int main () {
string str;
cout << "Enter a string: ";
getline (cin,str);
isPalindrome(str);
}
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000
bool isPalindrome(int x){
int c[MAX];
int i = 0;
int j;
int k = 0;
bool z;
if(x < 0){
return false;
}
while (x != 0){
int r = x % 10;
c[i] = r;
i++;
x = x / 10;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", c[j]);
}
for(k = 0; k <= (i / 2); k++){
if(c[k] == c[i - k - 1]){
z = true;
}
else
{
z = false;
}
}
return z;
}

Return all codes - String

Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric string S. Write a program to return the list of all possible codes that can be generated from the given string.
For most of the cases this code works but it gives wrong output for inputs which have numbers greater than 26. For eg: 12345.
#include <iostream>
#include <string.h>
using namespace std;
using namespace std;
int atoi(char a)
{
int i=a-'0';
return i;
}
char itoa(int i)
{
char c='a'+i-1;
return c;
}
int getCodes(string input, string output[10000]) {
if(input.size()==0)
{
return 1;
}
if(input.size()==1)
{
output[0]=output[0]+itoa(atoi(input[0]));
return 1;
}
string result1[10000],result2[10000];
int size2;
int size1=getCodes(input.substr(1),result1);
if(input.size()>1)
{
if(atoi(input[0])*10+atoi(input[1])>10&&atoi(input[0])*10+atoi(input[1])<27)
{
size2=getCodes(input.substr(2),result2);
}
}
for(int i=0;i<size1;i++)
{
output[i]=itoa(atoi(input[0]))+result1[i];
}
for(int i=0;i<size2;i++)
{
output[i+size1]=itoa(atoi(input[0])*10+atoi(input[1]))+result2[i];
}
return size1+size2;
}
int main(){
string input;
cin >> input;
string output[10000];
int count = getCodes(input, output);
for(int i = 0; i < count && i < 10000; i++)
cout << output[i] << endl;
return 0;
}
if i give input 12345, the output is:
"
abcde
awde
lcde
l"
instead of :
"
abcde
awde
lcde"
i got it fellow members. i did not initialised the size2 variable to zero. also i didn't use >= operator.
int getCodes(string input, string output[10000]) {
if(input.size()==0)
{
output[0]="";
return 1;
}
if(input.size()==1)
{
output[0]=itoa(atoi(input[0]));
return 1;
}
string result1[10000],result2[10000];
int size2=0;
int size1=getCodes(input.substr(1),result1);
if(input.size()>1)
{
if(atoi(input[0])*10+atoi(input[1])>=10&&atoi(input[0])*10+atoi(input[1])<27)
{
size2=getCodes(input.substr(2),result2);
}
}
int k=0;
for(int i=0;i<size1;i++)
{
output[k++]=itoa(atoi(input[0]))+result1[i];
}
for(int i=0;i<size2;i++)
{
output[k++]=itoa(atoi(input[0])*10+atoi(input[1]))+result2[i];
}
return k;
}
this is the final code for getCodes function. Thanks everyone :)
You can do that more simply with something like this:
#include <utility>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void getCodesRec(unsigned int num, string& current, vector<string>& result)
{
// First and last chars for the codes
static constexpr char FIRST_CHAR = 'a';
static constexpr char LAST_CHAR = 'z';
if (num == 0)
{
// When there is no more number add the code to the results
result.push_back(current);
}
else
{
// Add chars to the existing code
unsigned int next = num;
unsigned int rem = next % 10;
unsigned int f = 1;
// While we have not gone over the max char number
// (in practice this loop will run twice at most for a-z letters)
while (next > 0 && rem <= (unsigned int)(LAST_CHAR - FIRST_CHAR) + 1)
{
next = next / 10;
if (rem != 0) // 0 does not have a replacement
{
// Add the corresponding char
current.insert(0, 1, FIRST_CHAR + char(rem - 1));
// Recursive call
getCodesRec(next, current, result);
// Remove the char
current.erase(0, 1);
}
// Add another number
f *= 10;
rem += f * (next % 10);
}
}
}
vector<string> getCodes(unsigned int num)
{
vector<string> result;
string current;
getCodesRec(num, current, result);
return result;
}
int main()
{
unsigned int num = 12345;
vector<string> codes = getCodes(12345);
cout << "Codes for " << num << endl;
for (string& code : codes)
{
cout << "* " << code << endl;
}
return 0;
}
Output:
Codes for 12345
* abcde
* lcde
* awde

Deleting Duplicate chars from a string

Hello I'm kind of new to coding and I am writing a code that deletes any duplicate characters on a string. Let's say the input string was ABBA, then the output should be "empty" since all of the duplicates were deleted. Another example would be if the input string was KKCCD, then the output should be "D" as the code would remove the K and C duplicates. The problem with my code is that when I type KKCCD, it returns KCD and it doesnt delete the duplicates entirely. Also if I we're to type "AA" the result comes back as "A" instead of "Empty". Any help in fixing my code will be much apreciated. Thanks.
include
#include <string.h>
using namespace std;
string deduplicate(string input){
int i;
int x;
int len= input.length();
string outputStr;
string strEmpty = "Empty";
if (input.length() == 1)
{return input;}
for(i = 0; i<len;i++){
for(x = i+1; x<len; x++){
if(input[i] == input[x]){
input.erase(x,1);
x--;}
}
len = input.length();
}
return outputStr = input;
if (input.length() == 0)
{return strEmpty;}
return outputStr = input;
}
int main()
{
string input;
cout << "Enter a string: " << endl;
cin >> input;
cout << deduplicate(input);
return 0;
}
Change your duplicate function to this
string deduplicate(string input){
int i;
int x;
int len= input.length();
string outputStr;
string strEmpty = "Empty";
if (input.length() == 1)
{return input;}
for(i = 0; i<len;i++){
for(x = i+1; x<input.length(); x++){ // This line
if(input[i] == input[x]){
input.erase(x,1);
input.erase(i,1); // and this line
x--;
}
}
len = input.length();
}
return outputStr = input;
if (input.length() == 0)
{return strEmpty;}
return outputStr = input;
}
You did delete the duplicate character but not original, so the second erase statement does it.
You are deleting the duplicate you find, but you never delete the original character. The most efficient way to achieve your goal is to use STL algorithms. You might want to check out this link: Remove all duplicate characters from a string (STL)
Generally speaking I do not think if that is a good idea to delete characters from a string you are iterating. It would be much better and cleaner to construct your output instead:
#include <string>
#include <iostream>
using namespace std;
string deduplicate(string input) {
string outputStr;
if (input.length() == 1) {
return input;
}
for ( int i = 0; i < input.length(); i++ ) {
// try to find character at the rest of the string
if ( input.find( input[i], i+1 ) == string::npos ) {
// try to find character in the front
if ( ( i > 0 ) && ( input.rfind( input[i], i-1 ) == string::npos ) ) {
outputStr += input[i];
}
}
}
return outputStr;
}
int main()
{
string input;
cout << "Enter a string: " << endl;
cin >> input;
cout << deduplicate(input) << endl;
return 0;
}
Here is a "brute force" implementation.
string deduplicate(string str)
{
size_t l = str.length();
bool bFound = false;
char character = 0;
for (int i = 0; i < l; i++)
{
character = str[i];
while (true)
{
size_t next = str.rfind(character);
if (next != std::string::npos)
{
if (bFound || (!bFound && next != i))
{
str.erase(next, 1);
bFound = true;
l--;
}
}
if (next == i)
{
if (bFound == true)
{
i--;
}
bFound = false;
break;
}
}
}
return str;
}

split a char array by finding a token char

I cannot figure out why this function won't work.
I want to return a string what is trimmed from the char array passed into the function at the index where it finds an identifying char. Is there something obvious I'm missing? As it is, this only returns the first letter of the input char[]...
#include <iostream>
#include <cstring>
using namespace std;
string trim(char in[], char token){
char A[300];
for(int i = 0; i < strlen(in); i++){
if(in[i] != token){
A[i] = in[i];
} else
A[i] = '\0';
break;
}
return A;
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}
Because you break; in the first iteration. Use a block to avoid it.
#include <iostream>
#include <cstring>
using namespace std;
string trim(char in[], char token){
char A[300];
for(int i = 0; i < strlen(in); i++){
if(in[i] != token){
A[i] = in[i];
} else {
A[i] = '\0';
break;
}
}
return A;
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}
Note that calling strlen too many times isn't a good idea.
This should be better:
#include <iostream>
using namespace std;
string trim(char in[], char token){
char A[300];
bool token_found = false;
for(int i = 0; in[i] != '\0'; i++){
if(in[i] != token){
A[i] = in[i];
} else {
A[i] = '\0';
token_found = true;
break;
}
}
if (token_found) {
return A;
} else {
return in;
}
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}

going through a string of characters and extracting the numbers?

Given a string of characters, how can I go through it and assign all the numbers within that string into an integer variable, leaving out all other characters?
I want to do this task when there is a string of characters already read in through gets(), not when the input is read.
unsigned int get_num(const char* s) {
unsigned int value = 0;
for (; *s; ++s) {
if (isdigit(*s)) {
value *= 10;
value += (*s - '0');
}
}
return value;
}
Edit: Here is a safer version of the function.
It returns 0 if s is NULL or cannot be converted to a numeric value at all. It return UINT_MAX if the string represents a value larger than UINT_MAX.
#include <limits.h>
unsigned int safe_get_num(const char* s) {
unsigned int limit = UINT_MAX / 10;
unsigned int value = 0;
if (!s) {
return 0;
}
for (; *s; ++s) {
if (value < limit) {
if (isdigit(*s)) {
value *= 10;
value += (*s - '0');
}
}
else {
return UINT_MAX;
}
}
return value;
}
This is a simple C++ way to do that:
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
istringstream is("string with 123 embedded 10 12 13 ints", istringstream::in);
int a;
while (1) {
is >> a;
while ( !is.eof() && (is.bad() || is.fail()) ) {
is.clear();
is.ignore(1);
is >> a;
}
if (is.eof()) {
break;
}
cout << "Extracted int: " << a << endl;
}
}
Look up the strtol function from the standard C library. It allows you to find the part of a character array that is a number, and points to the first character that isn't a number and stopped the parsing.
You can use sscanf: it works like scanf but on a string (char array).
sscanf might be overkill for what you want though, so you can also do this:
int getNum(char s[])
{
int ret = 0;
for ( int i = 0; s[i]; ++i )
if ( s[i] >= '0' && s[i] <= '9' )
ret = ret * 10 + (s[i] - '0');
return ret;
}