C++ - Store part of string in another string [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 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);

Related

longest common substring in array of strings runtime problem [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 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.

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.

What is the difference between the two codes in C++? [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 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

simple C++ program to calculate number of instances of a substring [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
can someone point out the error in the following code . I am using a naive approach of comparing both strings character by charcter and updating variable 'u' then comparing it with length of substring . If this is true then variable 'c' is updated by one unit.
Program in C++ :
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char s[50],a[20];
cin.getline(s,50);
cin.getline(a,20);
//int l=strlen(s);
int p=strlen(a);
int i,c=0,j,u=0,k;
for(i=0;s[i]!='\0';i++)
{
if(a[i]='\0')
{break;}
if(s[i]==a[0])
{
for(j=i,k=0;a[k]!='\0';j++,k++)
{
if(s[j]==a[k])
{
u++;
//continue;
}
//else
//break;
}
//cout<<endl<<u;
if(u==p)
{
c++;
}
}
u=0;
}
cout<<endl<<"count "<<c;
getch();
}
For any kind of input combination , I am getting output as 0.
The problem is with this part:
if (a[i] = '\0')
{
break;
}
First, you are using = instead of ==, but that is not the entire problem. Either change a[i] to s[i], or comment out the entire block. I don't see why it is needed.
My tip and my coding convention to avoid your bug that you use = instead of == in:
if(a[i]='\0')
is to put the rvalue in the left side of the operand and the lvalue in the right side, like this:
if ('\0' == a[i])
this convention will avoid bugs like that(you will get a compilation error):
if ('\0' = a[i])
this code will generate a compilation error:
Error C2106 '=': left operand must be l-value

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.