dlang incompatible type error in checking string - d

i have this code to check if string have j character
import std.stdio;
void main() {
const string name = "john";
for (int i = 0;i < name.length;i++) {
if (name[i] == "j") {
writeln("the name variable contain character j");
}
}
}
and then i get an error
Performing "debug" build using /Library/D/dmd/bin/dmd for x86_64.
learning ~master: building configuration "application"...
source/app.d(36,13): Error: incompatible types for `(name[cast(ulong)i]) == ("j")`: `immutable(char)` and `string`
/Library/D/dmd/bin/dmd failed with exit code 1.

"j" is a string. 'j' is a char. name[i] is also a char, so you are comparing a char to a string which gives the error.

I would use indexOf for achieving the same thing. The indexOf Dlang function searches for a character in a range.

Related

How to convert a string to array of strings made of characters in c++?

How to split a string into an array of strings for every character? Example:
INPUT:
string text = "String.";
OUTPUT:
["S" , "t" , "r" , "i" , "n" , "g" , "."]
I know that char variables exist, but in this case, I really need an array of strings because of the type of software I'm working on.
When I try to do this, the compiler returns the following error:
Severity Code Description Project File Line Suppression State
Error (active) E0413 no suitable conversion function from "std::string" to "char" exists
This is because C++ treats stringName[index] as a char, and since the array is a string array, the two are incopatible.
Here's my code:
string text = "Sample text";
string process[10000000];
for (int i = 0; i < sizeof(text); i++) {
text[i] = process[i];
}
Is there any way to do this properly?
If you are going to make string, you should look at the string constructors. There's one that is suitable for you (#2 in the list I linked to)
for (int i = 0; i < text.size(); i++) {
process[i] = string(1, text[i]); // create a string with 1 copy of text[i]
}
You should also realise that sizeof does not get you the size of a string! Use the size() or length() method for that.
You also need to get text and process the right way around, but I guess that was just a typo.
std::string is a container in the first place, thus anything that you can do to a container, you can do to an
instance of std::string. I would get use of the std::transform here:
const std::string str { "String." };
std::vector<std::string> result(str.size());
std::transform(str.cbegin(), str.cend(), result.begin(), [](auto character) {
return std::string(1, character);
});

Using regex_match() on iterator with string in c++

working on a c++ project, I need to iterate on a string ( or char* depending the solution you could provide me ! ). So basically I'm doing this :
void Pile::evalExpress(char* expchar){
string express = expchar
regex number {"[+-*/]"};
for(string::iterator it = express.begin(); it!=express.end(); ++it){
if(regex_match(*it,number)){
cout<<*it<<endl;
}
}
}
char expchar[]="234*+";
Pile calcTest;
calcTest.evalExpress(expchar);
the iterator works well ( I can put a cout<<*it<<'endl above the if statement and I get a correct output )
and then when I try to compile :
error: no matching function for call to 'regex_match(char&, std::__cxx11::regex&)'
if(regex_match(*it,number)){
^
I have no idea why this is happening, I tried to don't use iterator and iterate directly on the expchar[i] but I have the same error with regex_match()...
Regards
Vincent
Read the error message! It tells you that you're trying to pass a single char to regex_match, which is not possible because it requires a string (or other sequence of characters) not a single character.
You could do if (std::regex_match(it, it+1, number)) instead. That says to search the sequence of characters from it to it+1 (i.e. a sequence of length one).
You can also avoid creating a string and iterate over the char* directly
void Pile::evalExpress(const char* expchar) {
std::regex number {"[+-*/]"};
for (const char* p = expchar; *p != '\0'; ++p) {
if (regex_match(p, p+1, number)) {
cout<<*p<<endl;
}
}
}

detecting negative integer in a string

Say I take in a string of input and I wanted to check if the user entered a negative number.
bool isNegative(string input[]) {
int i = 0;
if (input[i] == "-") {
return true;
} else {
return false;
}
}
I tried a boolean function to check if the first character is a - sign, representing negative numbers e.g -5, -25.
However, my Netbeans gave me this error:
main.cpp:39:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
anyone knows what this means?
There are two problems with your code:
You declare i that never changes. This is the same as doing input[0]
You compare a character to a string. Instead of "0" (double quotes) you need '0' (single quotes).
Fixing these two problems will fix your code.
Note: You can write this function in a single line:
bool isNegative(string input) {
return input[0] == '-';
}
You need to use single quotes, which represent a single character, rather than using double quotes, which represent a string.
if (input[i] == '-')

C++, Trouble with string and int conversion

I know how to convert the string when it's just made up of integers, or it begins with ints. I am trying to convert to an integer when the string starts with a char in the beginning, or middle. I've tried running through a for loop, checking if (isdigit(str[i]) before trying stoi, stringstream, atoi, etc... None of them really work. I have the same problem even without the for loop. I've tried Googling my problem, but no luck. Any suggestions, or anything that I can try?
You have to check character by character if it's a digit or not and, if it is, add it to a new string. In the end, you convert your new string to an int like you would normally. Look at the code below. Hope I could help!
string s = "pc2jjj10";
char temp;
string result;
for (int i = 0; i < s.length(); i++){
temp = s.at(i);
if (isdigit(temp)){
result.push_back(temp);
}
}
int number = stoi(result);

NTL String to ZZ conversion and ZZ to String

So I'm working on a basic RSA decryption program and I'm trying to figure out how to convert between a string and a ZZ. I've looked at the following question: How can i convert a string into a ZZ number?, however, I'm a little confused, and the answer there didn't work for me. My code:
fromBase()
{
string message = "hello world";
ZZ number (INIT_VAL, message.c_str());
cerr << number;
}
Gives me the following output.
bad ZZ input
Aborted
So, I thought, no big deal, I'll just try to find what INIT_VAL is supposed to be and that should give me an idea of where to look. But no such luck, I couldn't find anything that looked like it. I trued it with INIT_VAL_STRUCT as well, and got the following error:
base.cpp: In function âNTL::ZZ fromBase(std::string)â:
base.cpp:24: error: âmessageâ is not a type
base.cpp:24: error: expected â,â or â...â before â.â token
Lastly, I tried the solution posted here: Regarding create a NTL class type thinking I could try some type casting. Here's my code:
ZZ fromBase(string message)
{
ZZ x;
x = conv<ZZ>(message);
return x;
}
This gave me the following:
g++ base.cpp -lntl
base.cpp: In function âNTL::ZZ fromBase(std::string)â:
base.cpp:19: error: expected primary-expression before â>â token
As if I didn't specify a type.
To conclude, I know that INIT_VAL is a constant, but it doesn't seem to be working with something. I sense I've just got a disconnect, but trying to find it isn't easy. Any help would be appreciated, and any references for the NTL would be greatly appreciated. Sorry for the long post!
(Also, NTL is pretty poorly documented, from what I've seen, do you have any sites that may help a newbie to the library?)
You want to convert a string, that actually contains non numeric characters into a number.
There is no canonical number of a string, so you cannot do this in one step. C++ can give you the number of a character, which is the ascii number. You can use this function to get the ascii number of a string:
ZZ stringToNumber(string str)
{
ZZ number = conv<ZZ>(str[0]);
long len = str.length();
for(long i = 1; i < len; i++)
{
number *= 128;
number += conv<ZZ>(str[i]);
}
return number;
}
You get the string back by this function
string numberToString(ZZ num)
{
long len = ceil(log(num)/log(128));
char str[len];
for(long i = len-1; i >= 0; i--)
{
str[i] = conv<int>(num % 128);
num /= 128;
}
return (string) str;
}
If you have some non ascii characters like ö or ß you have to use an other way of converting a character to a number.(But I don't know how exactly this works)