longest common substring in array of strings runtime problem [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
string longestCommonPrefix(vector<string>& strs) {
string res = "";
int i, j;
bool flag = true;
for(int i=0; i<strs[0].size(); i++)
{
for(int j=0; j<strs.size()-1; j++)
{
if(strs[j][i] == strs[j+1][i])
{
flag = true;
}
else
return res;
}
if(flag == true)
{
res += strs[0][i];
}
}
return res;
}
I was doing this leetcode question where we had to find the longest common prefix of given array of strings and then i got stuck at this i cant understand what is the meaning of this error, most of the test cases are passed so i don't think logic is wrong.Is there any corner cases i am missing?
Runtime Error Message:
Line 924: Char 9: runtime error: reference binding to null pointer of type 'std::__cxx11::basic_string, std::allocator >' (stl_vector.h)
Last executed input:
[]
Thanks in advance

Its null pointer exception. So you should check if str is null i.e. str=='" for each string in vector.
and return answer accordingly.

Related

Parenthesis Checker gfgs [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
The following code is giving segmentation fault can anyone tell why. this is a geeks for geeks practice problem (Parenthesis Checker) .
Q- Given an expression string x. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
For example, the function should return 'true' for exp = “[()]{}{()()}” and 'false' for exp = “[(])”.
bool check(string s)
{
stack<char> save;
int x = s.size();
for(int i=0;i<x;i++)
{
if(s[i]=='{' || s[i]=='['|| s[i]=='(')
{
save.push(s[i]);
}
else if(s[i]=='}'){
if(save.top()=='{')
{
save.pop();
}
else{
return false;
}
}
else if(s[i]==']')
{
if(save.top()=='[')
{
save.pop();
}
else{
return false;
}
}
else if(s[i]==')')
{
if(save.top()=='(')
{
save.pop();
}
else{
return false;
}
}
}
if(!save.empty())
{
return false;
}
else{
return true;
}
}
You have a chance to seg-fault when the stack is empty and the next character in the string is a closing paren.
E.g. if the input string is ")" or "())" your program will not behave as intended.
You will need to change the if statements from
if (save.top() == '('){...} to if (!save.empty() && save.top() == '('){...}
And similarly for every other case where you check save.top (The preceeding code does not guarantee that the stack is not empty at that given point).
Note: You would also need to do this for each occurrence of pop as well, but the guards before top() will guarantee that the stack is not empty when you then pop.

runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff (basic_string.h) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The question is to find the largest common prefix strings amongst the array of string.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
This is what I have tried till now.
class Solution {
public:
string longestCommonPrefix(const vector<string>& v) {
if ( v.empty() )
{
return 0;
}
string smin = *min_element(v.begin(), v.end(),
[] (const std::string& s1, const std::string& s2) {
return s1.length() < s2.length(); }
);
string str="";
int i,j;
for(i=0; i<smin.size(); i++){
str+=smin[i];
for(j=0; j<v.size(); j++){
if(v[j].find(str)==string::npos){
str=str.substr(0, str.length() - 1);
return str;
}
}
}
return str;
}
};
Expected results have been given as an example above.
What I got as an error message is -:
Runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff
I have looked into similar answer for this and tried to implement them but the error isn't going. Please can somebody help me with this.
You have an empty string in the vector. At some point you calculate length minus 1. Then use it for the end index substr.
In general, you need to learn to use a debugger, which will tell you exactly what line you got the error and what the variables look like on that line.

C++ - Store part of string in another string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The problem is that in my code (which there is a loop getting alphabets of a string) it can not save the current string input into another variable:
Here is the code:
if(isalpha(Str[i])){
while (isalpha(Str[i])){
i++;
}
Str.erase( 0, i);
return 0;
}
I want to have another string (like temp ) to save each alphabet into the while loop. something like this:
if(isalpha(Str[i])){
string temp;
while (isalpha(Str[i])){
temp[i]=Str[i];
i++;
}
Str.erase( 0, i);
return 0;
}
can anyone help that what is the problem here?
As you declare temp as 0 length string, using temp[i] would be undefined behavior.
You may solve this problem by using
temp.push_back(Str[i]);
instead of
temp[i]=Str[i];
You're passing a position and count to std::string::erase. You can use the same parameters to construct another string with the characters that are about to be erased.
while (isalpha(Str[i])) {
i++;
}
string temp(Str, 0, i);
Str.erase(0, i);

incompatible types in assignment null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I Keep getting this error when I try to set the first char in firstname and lastname to a null and return it to a 1.
my code is...
int DeregisterStudent(int SID, struct studentdata SRecord[])
{
int index;
index = SRecordSearch(SID, MAXRECS, SRecord);
if(index >= 0)
{
SRecord[index].sid = 0;
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
return 1;
}
return 0;
}
and the error I get is error:incompatible type in assignment
for these two lines
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
You are not writing to the string correctly, it should be
SRecord[index].lastname[0] = '\0';
or
SRecord[index].lastname = "";
depending on how the struct was declared. In the second case you might be overwriting a dynamically allocated string pointer, in which case it should be free()ed.

Unexpected changing of C++ constant integer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The variable steady_counter is intialized as a constant integer.
cout << steady_counter;
So long as i have the above statement anywhere before the following code, the function runs as expected and checks if an integer input is or is not a runaround number.
The problem is that when the cout line is not present, the constant integer changes within the below if statements. I tested this by printing steady_counter before entering the if-else, and then after the if-else.
Without the cout line, steady_counter changes to a 4 digit number.
for (int i = 0; i < 10; i++)
{
if (CheckArr[i])
{
num_of_unique++;
}
}
if ((steady_counter == num_of_unique) & (final == NumArr[0]) )
{
return true;
}
else
{
return false;
}
}
Any idea what's going on? Why do I require a cout line to maintain the constant integer steady_counter?
One obvious problem:
for (int i = counter; i > 0; i --)
NumArr[i] = -1;
This covers values from 1 to counter inclusive; while valid indexes for NumArr are from 0 to counter-1 inclusive. So you write outside the array, corrupting something else; possibly another local variable.
Either correct the off-by-one error in the index
NumArr[i-1] = -1;
or use a more canonical loop
for (int i = 0; i < counter; ++i)
or, for more of a C++ flavour,
std::fill(NumArr, NumArr+counter, -1);
There are likely to be further errors, which are better found by using your debugger than by asking people to read through all your code.