Debug Assertion Failed error while comparing unicode using isdigit - c++

I have the debug assertion error in the if statement when i = 7:
Expression: c>= -1 && c <= 255
This is my code:
#include <iostream>
#include <string>
const char* clearString(std::string str)
{
for (int i = str.length() - 1; i >= 0; i--)
{
if ( !isdigit(str[i])
&& str[i] != ',')
{
str.erase(i, 1);
}
}
return str.c_str();
}
int main()
{
std::string str = "688,13 €";
std::cout << clearString(str);
}
I try to delete all characters in the string that are not numbers and ','.

For std::isdigit, see the Notes section as to why you are getting the assertion.
The fix is to cast to an unsigned char:
if (!isdigit(static_cast<unsigned char>(str[i]))
Second, your function returns the address of a local temporary, thus exhibits undefined behavior. Return a std::string instead.
std::string clearString(std::string str)
{
//…
return str;
}
Third, you could rewrite your function using std::remove_if and std::string::erase, instead of writing a loop that removes a character at a time.
#include <algorithm>
//...
std::string clearString(std::string str)
{
auto iter = std::remove_if(str.begin(), str.end(),
[&](char ch)
{ return !isdigit(static_cast<unsigned char>(ch)) && ch != ',';});
str.erase(iter, str.end());
return str;
}

The function isdigit() works only with chars that their decimal value is between -1 and 255.
The decimal value of the character € is -128 which the function doesn't support.
I would suggest to change the comparison instead of using isdigit(), compare the decimal values of the chars.
Change your function to this:
const char* clearString(std::string& str)
{
for (int i = str.length() - 1; i >= 0; i--)
{
if ((str[i] < '0' || str[i] > '9') && str[i] != ',')
{
str.erase(i, 1);
}
}
return str.c_str();
}

A little out of topic, about your algorithm.
It would be better if you don't erase every non-digit character, but shift your characters left, skipping all non-digits (except ',') and resize string.
About isdigit I would do how 0xBlackMirror suggested, compare to '0' and '9'.
Here is the code:
const char* clearString(std::string str)
{
int j = 0;
for (uint i = 0; i < str.size(); i++)
{
if ((str[i] >= '0' && str[i] <= '9') || str[i] == ',')
{
str[j++] = str[i];
}
}
str.resize(j);
return str.c_str();
}

Related

(C++) String to Double converter is inaccurate and totally wrong with negative numbers. What am i doing wrong?

Our teacher gave us this exercise:
"Given a string like '-5,14' write a function that returns the float value of -5,14
I used double here just to test the precision, but it also didn't work with floats.
[also i'm from Europe, we use the comma instead of the dot. Oh also we aren't allowed to use the type string and bool, we have to "make" them like in C]
This is what i came up with, and it seems to work a little bit. Positive numbers are similar, but wrong, and given a negative number, the result is similar to 10 times the positive of the given number.
It should work like this:
I read the string into an array of characters;
I check if the first character is a minus. if so, subtract 1 from the number of integer figures because i will count them later starting from index 0;
I count the number of integer figures with a loop from the start of the array to the ',' character;
I count the number of decimal figures with a loop from after the ',' to the end of the string;
[Keep in mind for the next step that, following the ASCII table, the code for the character of a number is that number + 48]
I add to the result variable every integer figure multiplied by ten to the power of whatever place in the number it has.
I do the same for the deicmal values but with the negative exponent.
if the number was negative, i multiply the result with -1.
But for some reason it's not working properly. The lower the number is, the less accurate it is (given 4,5 the result is 9, but given 345,543 the result is 350,43)
#include <iostream>
#define EOS '\0'
#define DIM 100
#define TRUE 1
#define FALSE 0
void leggiN(char* c)
{
std::cout << "Insert a number: ";
std::cin >> c;
}
double stof(char* str)
{
double Result = 0;
double ascii_to_int = 48;
int i = 0;
int j = 0;
int IntegerDigits = 0;
int DecimalDigits = 0;
int CommaIndex;
int isNegative = FALSE;
if (str[0] == '-')
{
IntegerDigits = -1;
isNegative = TRUE;
}
while (str[i] != ',')
{
++IntegerDigits;
++i;
}
CommaIndex = i;
++i;
while (str[i] != EOS)
{
++DecimalDigits;
++i;
}
for (i = (CommaIndex - 1); i >= 0; --i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, j));
++j;
}
j = 0;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
if (isNegative == 1)
Result = Result * -1;
return Result;
}
int main()
{
char str[DIM];
leggiN(str);
std::cout << stof(str);
}
use j = 1 to start your second for loop. You are trying to raise 10 to the power of -0
j = 1;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
If your code return 9.0 when you enter "4,5", your problem has nothing to do with imprecision.
There are other problems in your code, I've tried to un it and got a SEGFAULT...
#include <iostream>
#define EOS '\0' // 0 being such a special value, there is no need to
// define a named constant for it.
#define DIM 100
#define TRUE 1 // the language defines boolean values, avoid defining
#define FALSE 0 // unnecessary named constants for something that already
// exists.
void leggiN(char* c)
{
std::cout << "Insert a number: ";
std::cin >> c; // Inserting from cin to a char* is a BIG no-no.
// some compilers won't even allow it, for good reasons
// i.e.: what is the length of the array pointed to?
}
double stof(char* str) // you are indicating that you may modify str?
{
double Result = 0;
double ascii_to_int = 48; // this is a terrible name.
int i = 0;
int j = 0;
int IntegerDigits = 0;
int DecimalDigits = 0;
int CommaIndex;
int isNegative = FALSE;
if (str[0] == '-') // is str a valid pointer? what happens if NULL ??
{
IntegerDigits = -1;
isNegative = TRUE;
// you fail to skip the sing character, should have ++i here.
}
while (str[i] != ',') // what happens if there is no ',' in the string?
{ // you should check for str[i] == 0.
++IntegerDigits;
++i;
}
CommaIndex = i;
++i;
while (str[i] != EOS)
{
++DecimalDigits; // why do you count decimal digits?
++i; // you do not use this result anyway...
}
for (i = (CommaIndex - 1); i >= 0; --i)
{
// what happens if you have non-digit characters? they participate
// in the conversion??
// you call std::pow(), but do not include <cmath> at the top of the file.
// isn't str[i] - '0' clearer ?
Result += (str[i] - ascii_to_int) * (std::pow(10, j));
++j;
}
j = 0;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
if (isNegative == 1) // you had defined constants fot this, but don't use them.
Result = Result * -1;
return Result;
}
int main()
{
char str[DIM];
leggiN(str);
std::cout << stof(str);
}
Here is one way to achieve what you want.
#include <iostream>
#include <string>
const char DECIMAL_POINT = ','; // we'll use a named constant here....
// usually, we'd have to check the locale
// for regional specific information.
// works like atod(), conversion stops at end of string of first illegal character.
double stof(const char* str) {
// check input, must be not null, not empty
if (!str || str[0] == 0)
return 0;
int i = 0;
bool isNegative = false;
// take care of leading sign
if (str[0] == '-' || str[0] == '+') {
isNegative = (str[0] == '-');
++i;
}
// convert integer part.
double result = 0;
while ('0' <= str[i] && str[i] <= '9') {
result = (result * 10) + (str[i] - '0');
++i;
}
// only do decimals if they are there.
if (str[i] != DECIMAL_POINT)
return (isNegative) ? -result : result;
++i; // skip decimal point
double decimals = 0;
double multiplier = .1;
while ('0' <= str[i] && str[i] <= '9') {
decimals += (str[i] - '0') * multiplier;
++i;
multiplier *= .1;
}
result += decimals;
return (isNegative) ? -result : result;
}
int main() {
// always use std::string to read strings from cin.
std::string str;
std::cout << "Insert a number: ";
std::cin >> str;
std::cout << "in: " << str << " out: " << stof(str.c_str()) << '\n';
return 0;
}

what is wrong in my logic reversing vowels in a string?

I tried solving a problem in leetcode
which asks the programmer to reverse the vowels in the given string.
When I wrote my code in C, it ran fine and passed all the test cases.
I tried writing the same code in C++ but for a particular test case, it failed.
bool isVowel(char a)
{
if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return true;
if(a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
return true;
return false;
}
class Solution {
public:
string reverseVowels(string s) {
int i, j, k;
int len = s.length();
j = s.length() - 1;
i = 0;
k = 0;
string result;
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result[k] = s[j];
k++;
}
j--;
}
k = 0;
j = s.length() - 1;
while (i <= j) {
if(isVowel(s[i])) {
s[i] = result[k];
k++;
}
i++;
}
return s;
}
};
For some reason, when the input is "A new order began, a more Roman age bred Rowena." there is an error message AddressSanitizer: stack-buffer-overflow on address 0x7ffd4a543ab0 at pc 0x000000405efb.
When I tried to debug, I found that, the first while loop gets infinite. But when I replace the string result to char result[len], my code is working fine.
What is wrong in my approach?
Thanks
hago
You may not use the subscript operator for an empty string to change its value.
So your program has undefined behavior.
Pay attention to that in any case you are not reversing vowels in a string. You are trying to create a new string with reversed vowels from a given string. But this is not the same thing.
I can suggest the following Solution.:)
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
class Solution final
{
private:
static bool isVowel( char c )
{
const char *vowels = "AEIOU";
return std::strchr( vowels, std::toupper( static_cast<unsigned char>( c ) ) );
}
public:
static std::string & reverseVowels( std::string &s )
{
auto first = std::begin( s ), last = std::end( s );
do
{
while ( first != last && !isVowel( *first ) ) ++first;
if ( first != last )
{
while ( --last != first && !isVowel( *last ) );
}
if ( first != last ) std::iter_swap( first++, last );
} while ( first != last );
return s;
}
};
int main()
{
std::string s( "I am trying to write a program in C++" );
std::cout << s << '\n';
std::cout << Solution::reverseVowels( s ) << '\n';
return 0;
}
The program output is
I am trying to write a program in C++
i am tryong ta wreti o prigram In C++
Pay into account that the letter 'y' is not included in the set of vowels.
Your solution is correct but with a simple mistake.
When you declare string result; then this variable is declared with 0 size. So whenever you try to place character at some position (i.e result[0], result[1], ...) it finds that there is no allocated memory for this variable. So it throws error.
In stead of placing character to the result, you can add the character to this string.
So you can write result = result + s[j];
Code snap should be like this -
string result = "";
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result = result + s[j];
}
j--;
}
But adding character to a string takes more run-time.
Besides this, you can also use string.push_back() to add a single character to a string. It's complexity is overall O(n), n = length of the final string.
string result = "";
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result.push_back(s[j]);
}
j--;
}

C++ split string to vector<string>

The function I am using is
vector<string> tokenise(string s){
}
Firstly, I intend to split the string into substrings, in which case the string is always arithmetic expression (e.g. "100+5") and there could be some whitespaces.
"100+5" is needed to convert to "100", "+", "5"
After conversion, the substrings will be stored in a vector and return it. I am struggling with the fist step and using the subscript to loop over a string. The type of the value returned is char, so there is no way to put it in the vector.
You could just call the string's substring method, after figuring out the range of characters that are either digits, arithmetic characters, or unwanted.
You mentioned - The type of the value returned is char, so there is no way to put it in the vector.
You have some function that returns a character. You want to then insert the equivalent string into the vector.
Assuming your vector is defined as
std::vector<std::string> broken_strings;
So you can do it as follows.
char ch = ...; // Here comes the character that you get from the function.
std::string str(1, ch);
broken_strings.push_back(str);
Then you can return broken_strings.
Edit:
OP mentions that he wants to tokenize algebraic expressions.
So it will have to be done it a different way.
Following is a simple approach.
std::vector<std::string> broken;
std::string temp;
for ( int i = 0; i<s.length() ;i++){
char ch = s[i];
if (ch == ' ')
continue;
else if (ch >= '0' && ch <='9')
temp += ch;
else{
if (temp.length() != 0)
broken.push_back(temp);
temp = "";
temp += ch;
broken.push_back(temp);
temp = "";
}
}
if (temp.length() != 0)
broken.push_back(token);
return broken;
You can see the demo of the same here
Ideone
vector<string> tokenise(string s)
{
vector<string> v;
string number;
for(int i = 0; i < s.length(); ++i)
{
if((s[i] >= '0') && (s[i] <= '9'))
{
number += string(1, s[i]);
}
else if(s[i] == '.')
{
number += string(1, s[i]);
}
else if((s[i] == '+') || (s[i] == '-') || (s[i] == '*') || (s[i] == '/'))
{
if(number.size())
{
v.push_back(number);
number.clear();
}
v.push_back(string(1, c));
}
}
if(number.size())
{
v.push_back(number);
number.clear();
}
return v;
}

Sorting a vector of strings by the first letter in non-ascii order in C++

I have a text file with a list of words.
I used ifstream to read these words into a vector and now I am trying to sort them in an order similar to:
A a B b C c [...]
I tried to implement this using a third for loop inside of a bubble search algorithm to look at the first character of each word (I know this is far from the most efficient way especially if I was using a large data set)
And then check whether the letter and the next letter were uppercase or lowercase and switching if the uppercase letter was the same letter as the current letter, but this didn't seem to work.
void bubble_Sort (vector <string> & words)
{
for (unsigned i = words.size(); i >= 2; --i)
{
for (unsigned k = 0; k + 1 < i; k++)
{
int hi = k+1;
string temp1 = words[hi];
string temp2 = words[k];
int smallsize = words[hi].size();
int smallprecedence = 0;
if (words[k].size() < words[hi].size())
smallsize = words[k].size();
for (unsigned j = 0; j < smallsize; j++)
{
if (temp1[j] >= 'A' && temp1[j] <= 'Z')
{
if (temp2[j] >='a' && temp2[j] <= 'z')
{
char lowercase1 = temp1[j] + 32;
if (lowercase1 == temp2[j])
{
string temp = words[k];
words[k] = words[hi];
words[hi] = temp;
break;
}
}
else if (temp2[j] >= 'A' && temp2[j] <= 'Z')
{
if (temp1[j] < temp2[j])
{
string temp = words[k];
words[k] = words[hi];
words[hi] = temp;
break;
}
}
}
if (temp1[j] >= 'a' && temp1[j] <= 'z')
{
if (temp2[j] >= 'A' && temp2[j] <= 'Z')
{
char uppercase1 = temp1[j] - 32;
if (uppercase1 < temp2[j])
{
string temp = words[k];
words[k] = words[hi];
words[hi] = temp;
break;
}
}
else if (temp2[j] >= 'a' && temp2[j] <= 'z')
{
if (temp1[j] < temp2[j])
{
string temp = words[k];
words[k] = words[hi];
words[hi] = temp;
break;
}
}
}
else if (temp1[j] == temp2[j] && temp1.size() < temp2.size())
++smallprecedence;
}
if (smallprecedence == smallsize)
{
string temporary = words[k];
words[k] = words[hi];
words[hi] = temporary;
}
}
}
}
Don't reinvent the wheel. Just modify the default comparison function so aA < bB (regardless of case) and A < a.
EDIT I used the wrong comparison function. It should return true for <, and false for >=. This has been fixed
std::vector<std::string> vec;
//
std::sort(vec.begin(), vec.end(), [](const std::string& lhs, const std::string& rhs)
{
const char* s1=lhs.c_str();
const char* s2=rhs.c_str();
while(true) {
// first ignore case
if ( std::toupper(*s1) < std::toupper(*s2) ) return true;
if ( std::toupper(*s1) > std::toupper(*s2) ) return false;
// end of both strings, exact match
if ( *s1 == 0 && *s2 == 0 ) return false;
// compare upper case vs lower case ('A' vs 'a')
if ( *s1 > *s2) return false;
if ( *s1 < *s2) return true;
++s1; ++s2;
}
});
First, get rid of the hard-coded ASCII-isms. C and C++ have long had functions for determining whether a character is a letter, a digit, uppercase, lowercase, etc. Look them up.
Second, describe clearly what goes into determining the order that you want the result to be in.
Third, from that description, write a function that takes two strings, and tells you whether the first string should come before the second. Use that function in the sort.
You can sort a vector using std::sort and get a reference to the first character in a std::string using std::string::at() :
std::vector<std::string> vec;
//
std::sort(vec.begin(), vec.end(), [](const std::string& lhs, const std::string& rhs)
{
char l_ch, r_ch;
l_ch = lhs.at(0);
r_ch = rhs.at(0);
return l_ch < r_ch;
});
I think it's really enough to skip exactly equal prefixes and then compare once with uppercasing:
std::vector<std::string> vec;
//
std::sort(vec.begin(), vec.end(), [](const std::string& lhs, const std::string& rhs)
{
const char* s1=lhs.c_str();
const char* s2=rhs.c_str();
while(*s1 && *s1 == *s2) {++s1; ++s2;}
int rc = toupper(*s1) - toupper(*s2);
if (rc) return rc;
return *s1 - *s2;
});
If you need to compare by first letter only, simply remove while(*s1 && *s1 == *s2) {++s1; ++s2;}

String error output [duplicate]

This question already has answers here:
C++ Remove punctuation from String
(12 answers)
Closed 9 years ago.
I got a code. It should give me an output that will erase the middle character between 'z' and 'p'. for example: zipZap("zipXzap"): expected [zpXzp] but found [z pXz p]
std::string zipZap(const std::string& str){
string a = str;
string b = "";
size_t len = str.length();
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a[i+1] = ' ';
}
return a;
}
When i replaced a[i+1] = ''; it gave me an error.
You are not removing the chars, you are replacing them with ' '.
There are many ways to do this. One simple way is to build a new string, only adding chars when the proper conditions are met:
std::string zipZap(const std::string& str)
{
string a;
size_t len = str.length();
for (size_t i = 0; i < len; i++) {
// Always add first and last chars. As well as ones not between 'z' and 'p'
if (i == 0 || i == len-1 || (str[i-1] != 'z' && str[i+1] != 'p')) {
a += str[i];
}
}
return a;
}
Use string.erase() :
std::string zipZap(const std::string& str){
std::string a = str;
std::string b = "";
size_t len = str.length();
for (size_t i = 0; i < len; i++){
if (a[i] == 'z')
if (a[i+2] == 'p')
a.erase(i+1,1);
}
return a;
}
You're completely right that you cant replace an element of the string with ''.
A string is an array of chars, and '' is not a char at all. It is nothing.
If we look at the cplusplus page for a string
http://www.cplusplus.com/reference/string/string/
We see that we can use erase(iterator p) to "Erase characters from string (public member function)"
So if we change:
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a.erase(a.begin() + i + 1);
We're closer now, but we can see that len is no longer the same as str.length(). the length of a is now actually 1 char shorter than len. To remedy this however we can simply add:
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a.erase(a.begin() + i + 1);
len -= 1;
Hope that helps
If you #include <regex>, you can do a regular expression replacement.
std::string zipZap(const std::string& str){
regex exp("z.p");
string a = str;
a = regex_replace(a, exp "zp");
return a;
}