I have a function that takes two strings and determines if they are the same. I am trying to tokenize the string and combine all of tokens into one string. This is what I have so far and I am getting Bus error :10.
any help appreciated.
#include <iostream>
#include <string>
using namespace std;
bool stringCheck(string s1, string s2){
string strCheck1 = "";
string strCheck2 = "";
char *cstr1 = new char[s1.length()]; // char array with length of string
strcpy(cstr1, s1.c_str()); // copies characters of string to char array
char *cstr2 = new char[s2.length()];
strcpy(cstr2, s2.c_str());
char *p1 = strtok(cstr1, " "); // creates a char array that stores token that
// is delimeted
cout << "p1 " << p1 << endl; ///outputs token that is found
strCheck1.append(p1); // appends token to string
cout << "strCheck1 " << strCheck1 << endl; // outputs string
while(p1 != NULL) // while the token is not a null character
{
cout<<"parsing" << endl;
p1 = strtok(NULL, " "); // continue to parse current string.
cout << "p1 " << p1 << endl;
strCheck1.append(p1);
cout << "str1 " << strCheck1 << endl;
}
char * p2 = strtok(cstr2, " ");
cout << "p2 " << p2 << endl;
strCheck2.append(p2);
cout << "strCheck2 " << strCheck2 << endl;
while(p2 != null){
p2 = strtok(NULL, " ");
strCheck2.append(p2);
cout << "str2 " << strCheck2 << endl;
}
if( strCheck1.compare(strCheck2) != 0)
{
return 0;
}
else return 1;
}
int main(void){
string s1 = "jam yoooo jay";
string s2 = "jam yoooo";
if(stringCheck(s1, s2) == 1){
cout << "strings same"<< endl;;
}
else{
cout << "strings not same" << endl;
}
}
is there a conditional statement I could pair up with
while(p1 != NULL)
I know this is a pretty silly function but just trying to polish up my skills. any help appreciated!
There are some things you must change:
char *cstr1 = new char[s1.length()];
c-string are null-terminated, so you need one more char to store the null character:
char *cstr1 = new char[s1.length() + 1];
(same for cstr2)
strCheck1.append(p1)
p1 cannot be a null pointer (see Assign a nullptr to a std::string is safe? for further details). So you have to check...
if (p1) strCheck1.append(p1);
(same for p2).
cout << p1 << endl
if p1 is a null pointer bad things can happen (see Why does std::cout output disappear completely after NULL is sent to it). So you have to check...
if (p1) { cout << "p1 " << p1 << endl; strCheck1.append(p1); }
(same for p2)
there is a memory leak (cstr1 / cstr2 must be deleted).
At the end it should work.
Probably you should consider other systems to extract tokens (where you haven't to mix std::string and c-string). E.g.:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string text("text-to-tokenize");
std::istringstream iss(text);
std::string token;
while(getline(iss, token, '-'))
std::cout << token << std::endl;
return 0;
}
Related
I am trying to solve this problem:
https://www.geeksforgeeks.org/count-possible-decodings-given-digit-sequence/
Example:
Input:
Input str = "121"
Total decoding:: 3 :: ABA AU LA
I am able to code this problem through recursion. But the code fails to process a bigger input sequence (for e.g., i/p str = 11111111111111111111111111111111111111111)
This is happening because I am calculating sub-problems again-and-again.
Can anyone help me by letting me know how to memoize below sample code?
PS - I know there are other ways to solve this problem. But I don't want to do that. I want to memoize this solution only. It will help me to build my concept. Please help.
Here is the code:
#include "iostream"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
namespace solution3
{
void solve(string str, string& out, vector<string>& v)
{
if (str.size() == 0)
{
v.push_back(out);
return;
}
//we have 2 choices:
//ch#1: take 1st char of str
//ch#2: take 1st and 2nd chars of str
if (str.size() >= 1)//ch#1: take 1st char of str
{
string out1 = out;
string str1 = str;
int num1 = stoi(str.substr(0, 1)); // converting string at index 0 to integer
if (num1) // we will not consider if the string at index 0 is zero.
{
out1.push_back(('#' + num1)); //<-- It will conevrt 1 into A; 2 into B; and so on.
str1 = str1.erase(0, 1);//erase the index 0 from str1.
solve(str1, out1, v);
}
}
if (str.size() >= 2)//ch#2: take 1st and 2nd chars of str
{
string out2 = out;
string str2 = str;
int num2 = stoi(str.substr(0, 2)); // converting string at index 0 and 1 to integer
// checking if num2 is a valid number for decoding.
// num2 should be - NON-ZERO, 1st char is not ZERO, is within the range of 1 and 26.
if (num2 && str[0] != '0' && num2 > 0 && num2 <= 26)
{
out2.push_back(('#' + num2));
//Erase 1st two chars from str
str2 = str2.erase(0, 1);//erase the index 0 from str1.
str2 = str2.erase(0, 1);//erase the index 0 from str1.
solve(str2, out2, v);
}
}
}
void alphacode(string str)
{
string out;
vector<string> v; //<-- To store all the Decodings
solve(str, out, v);
cout << "Total decoding:: " << v.size() << ":: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
}
int main()
{
string str = "25114";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "3333333333";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "202";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "2010";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111111111111111111111111"; //<-- takes too much time! How to solve this?
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
return 0;
}
You can memoize each substring that you are currently working with, which you're forming after deleting one or two characters, depending on the case. Something like this:
#include "iostream"
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
map<string, vector<string>> dp;
namespace solution3
{
void solve(string str, string& out, vector<string>& v)
{
if (str.size() == 0)
{
v.push_back(out);
return;
}
//we have 2 choices:
//ch#1: take 1st char of str
//ch#2: take 1st and 2nd chars of str
if(dp.find(str) != dp.end()) {
vector<string> current = dp[str];
for(string s: current) {
v.push_back(s);
}
return;
}
if (str.size() >= 1)//ch#1: take 1st char of str
{
string out1 = out;
string str1 = str;
int num1 = stoi(str.substr(0, 1)); // converting string at index 0 to integer
if (num1) // we will not consider if the string at index 0 is zero.
{
out1.push_back(('#' + num1)); //<-- It will conevrt 1 into A; 2 into B; and so on.
str1 = str1.erase(0, 1);//erase the index 0 from str1.
solve(str1, out1, v);
}
}
if (str.size() >= 2)//ch#2: take 1st and 2nd chars of str
{
string out2 = out;
string str2 = str;
int num2 = stoi(str.substr(0, 2)); // converting string at index 0 and 1 to integer
// checking if num2 is a valid number for decoding.
// num2 should be - NON-ZERO, 1st char is not ZERO, is within the range of 1 and 26.
if (num2 && str[0] != '0' && num2 > 0 && num2 <= 26)
{
out2.push_back(('#' + num2));
//Erase 1st two chars from str
str2 = str2.erase(0, 1);//erase the index 0 from str1.
str2 = str2.erase(0, 1);//erase the index 0 from str1.
solve(str2, out2, v);
}
}
dp[str] = v;
}
void alphacode(string str)
{
string out;
vector<string> v; //<-- To store all the Decodings
solve(str, out, v);
cout << "Total decoding:: " << v.size() << ":: ";
// for (int i = 0; i < v.size(); i++)
// cout << v[i] << " ";
cout << endl;
}
}
int main()
{
string str = "25114";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "3333333333";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "202";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "2010";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111111111111111111111111"; //<-- takes too much time! How to solve this?
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
return 0;
}
I have 20byte binary char array. I want to divide into 3 parts: 4byte, 8byte, 8byte. I implemented it like the following. It works but seems I might be able to use buffer stream. I want to know how to use it.
Now
void main()
{
// _data is 20byte binary char array. 0000000000000000000000000000000000000000000001111001110001111111001110000010110000001011101101000000000000000000000000000000000000000000000000000000000000000001
// strA (4 byte)
string strA;
for (std::size_t i = 0; i < 4; ++i) {
strA += bitset<8>(_data.c_str()[i]).to_string();
}
cout << strA << endl; // 00000000000000000000000000000000
// strB (8 byte)
string strB;
for (std::size_t i = 4; i < 12; ++i) {
strB += bitset<8>(_data.c_str()[i]).to_string();
}
cout << strB << endl; // 0000000000000111100111000111111100111000001011000000101110110100
// strC (8 byte)
string strC;
for (std::size_t i = 12; i < 20; ++i) {
strC += bitset<8>(_data.c_str()[i]).to_string();
}
cout << strC << endl; // 0000000000000000000000000000000000000000000000000000000000000001
}
Expectation
I want to implement like this.
void main()
{
stringstream ss = _data;
strA = ss.pop(4);
strB = ss.pop(8);
strC = ss.pop(8);
}
Update 1
Thank you guys. I'm trying all of answers you gave me one by one. I'm newbie in c++ so it takes time to understand it. The following is Anders K's one.
struct S { char four[4]; char eight1[8]; char eight2[8]; };
struct S *p = reinterpret_cast<S*>(&_data);
cout << p->four << endl; // => Output "(" I think I can find way to output
Update 2
It works using string::substr. Thanks Zakir.
int main()
{
// I don't know how to change to string value in smart way..
string str;
for (std::size_t i = 0; i < _data.size(); ++i) {
str += bitset<8>(_data.c_str()[i]).to_string();
}
cout << str << endl; // 0000000000000000000000000000000000000000000001111001110001111111001110000010110000001011101101000000000000000000000000000000000000000000000000000000000000000001
std::string d = str; // Your binary stream goes here
int lenA = (4*8); // First 4 Bytes
int lenB = (8*8); // Second 8 Bytes
int lenC = (8*8); // Last 8 Bytes
std::string strA = d.substr(0, lenA);
std::string strB = d.substr(lenA + 1, lenB - 1);
std::string strC = d.substr(lenA + lenB + 1, lenC - 1);
cout << strA << endl; // 00000000000000000000000000000000
cout << strB << endl; // 000000000000111100111000111111100111000001011000000101110110100
cout << strC << endl; // 000000000000000000000000000000000000000000000000000000000000001
}
Update 3
I got an error when I try Scheff's way. This is my fault and I think I can solve it. And I think I should reconsider about _data's type.
int main
{
const char data = _data;
const char *iter = data;
string strA = pop(iter, 4);
string strB = pop(iter, 8);
string strC = pop(iter, 8);
cout << "strA: '" << strA << "'" << endl;
cout << "strB: '" << strB << "'" << endl;
cout << "strC: '" << strC << "'" << endl;
}
Make Error Message
error: no viable conversion from 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to
'const char'
const char data = _data;
It is not possible to make a new method for std::stringstream. (At least, I would not recommend this.)
Instead, I would suggest to make it a function. The usage would be similar.
#include <bitset>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string pop(istream &in, size_t n)
{
string ret;
while (n--) {
unsigned char byte = (unsigned char)in.get();
ret += bitset<8>(byte).to_string();
}
return ret;
}
int main()
{
string data(
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\xde\xad\xbe\xef\x00", 20);
istringstream in; in.str(data);
string strA = pop(in, 4);
string strB = pop(in, 8);
string strC = pop(in, 8);
cout << "strA: '" << strA << "'" << endl;
cout << "strB: '" << strB << "'" << endl;
cout << "strC: '" << strC << "'" << endl;
return 0;
}
Output:
strA: '00010001001000100011001101000100'
strB: '0101010101100110011101111000100010011001101010101011101111001100'
strC: '1101110111101110111111111101111010101101101111101110111100000000'
Note:
Using a std::istream makes it applicable to any stream derived from std::istream.
There is no error handling in pop(). Thus, the returned result of pop() might be wrong if the passed stream isn't good() afterwards.
Btw. I agree with the comments that a std::stream might be "over-engineered". Thus, here the "light-weight" version:
#include <bitset>
#include <iostream>
#include <string>
using namespace std;
string pop(const char *&iter, size_t n)
{
string ret;
while (n--) {
ret += bitset<8>((unsigned char)*iter++).to_string();
}
return ret;
}
int main()
{
const char data[] =
"\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
"\xbb\xcc\xdd\xee\xff\xde\xad\xbe\xef\x00";
const char *iter = data;
string strA = pop(iter, 4);
string strB = pop(iter, 8);
string strC = pop(iter, 8);
cout << "strA: '" << strA << "'" << endl;
cout << "strB: '" << strB << "'" << endl;
cout << "strC: '" << strC << "'" << endl;
return 0;
}
The output is identical like above.
Note:
The usage of char[] and char* is much more sensitive for out-of-bound access. Thus, it has to be used carefully.
I'm not quite sure whether the (unsigned char) cast is necessary. As I have often seen "funny" effects concerning char, int and sign extension, I guess it cannot hurt. (I feel better with it.)
I can propose you a very simple alternative using string::substr
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string _data="00010001001000100011001101000100\
0101010101100110011101111000100010011001101010101011101111001100\
1101110111101110111111111101111010101101101111101110111100000000";
int lenA = (4*8); //First 4 Bytes
int lenB = (8*8); //Second 8 Bytes
int lenC = (16*8); //Last 16 Bytes
string strA = _data.substr(0, lenA - 1);
string strB = _data.substr(lenA, lenB - 1);
string strC = _data.substr(lenB, lenC - 1);
std::cout << "strA: " << strA << endl;
std::cout << "strB: " << strB << endl;
std::cout << "strC: " << strC << endl;
return 0;
}
This is neat and simple but gets your job done!
Demo here
Output:-
strA: 0001000100100010001100110100010
strB: 010101010110011001110111100010001001100110101010101110111100110
strC: 100110011010101010111011110011001101110111101110111111111101111010101101101111101110111100000000
So in this program I'm trying to go through word by word and make it only lowercase letters, no whitespace or anything else. However, my string "temp" isn't holding anything in it. Is it because of the way I'm trying to modify it? Maybe I should try using a char * instead? Sorry if this is a stupid question, I'm brand new to c++, but I've been trying to debug it for hours and can't find much searching for this.
#include <string>
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;
int main(int argc, char* argv[]) {
/*if (argc != 3) {
cout << "Error: wrong number of arguments." << endl;
}*/
ifstream infile(argv[1]);
//infile.open(argv[1]);
string content((std::istreambuf_iterator<char>(infile)),
(std::istreambuf_iterator<char>()));
string final;
string temp;
string distinct[5000];
int distinctnum[5000] = { 0 };
int numdist = 0;
int wordcount = 0;
int i = 0;
int j = 0;
int k = 0;
int isdistinct = 0;
int len = content.length();
//cout << "test 1" << endl;
cout << "length of string: " << len << endl;
cout << "content entered: " << content << endl;
while (i < len) {
temp.clear();
//cout << "test 2" << endl;
if (isalpha(content[i])) {
//cout << "test 3" << endl;
if (isupper(content[i])) {
//cout << "test 4" << endl;
temp[j] = tolower(content[i]);
++j;
}
else {
//cout << "test 5" << endl;
temp[j] = content[i];
++j;
}
}
else {
cout << temp << endl;
//cout << "test 6" << endl;
++wordcount;
final = final + temp;
j = 0;
for (k = 0;k < numdist;k++) {
//cout << "test 7" << endl;
if (distinct[k] == temp) {
++distinctnum[k];
isdistinct = 1;
break;
}
}
if (isdistinct == 0) {
//cout << "test 8" << endl;
distinct[numdist] = temp;
++numdist;
}
}
//cout << temp << endl;
++i;
}
cout << wordcount+1 << " words total." << endl << numdist << " distinct words." << endl;
cout << "New output: " << final << endl;
return 0;
}
You can't add to a string with operator[]. You can only modify what's already there. Since temp is created empty and routinely cleared, using [] is undefined. The string length is zero, so any indexing is out of bounds. There may be nothing there at all. Even if the program manages to survive this abuse, the string length is likely to still be zero, and operations on the string will result in nothing happening.
In keeping with what OP currently has, I see two easy options:
Treat the string the same way you would a std::vector and push_back
temp.push_back(tolower(content[i]));
or
Build up a std::stringstream
stream << tolower(content[i])
and convert the result into a string when finished
string temp = stream.str();
Either approach eliminates the need for a j counter as strings know how long they are.
However, OP can pull and endrun around this whole problem and use std::transform
std::transform(content.begin(), content.end(), content.begin(), ::tolower);
to convert the whole string in one shot and then concentrate on splitting the lower case string with substring. The colons in front of ::tolower are there to prevent confusion with other tolowers since proper namespacing of the standard library has been switched off with using namespace std;
Off topic, it looks like OP is performing a frequency count on words. Look into std::map<string, int> distinct;. You can reduce the gathering and comparison testing to
distinct[temp]++;
I am trying to write a program that will parse a string and give each position of word. I cannot figure out why I am getting a
"DEBUG ASSERTION FAILED" Experssion: Invalid null pointer
window when it reaches the last word of the string.
char * pointer_char;
int pos = 0;
std::string str = "This test string will fail at this word..!. ";
int i = 0;
int length = str.length();
char * c = new char [str.size()+1];
std::copy(str.begin(), str.end(), c);
c[str.size()] = '\0';
cout << "Testing string is " << str << endl << endl;
pointer_char = strtok (c," ");
while(pointer_char != NULL)
{
cout << pointer_char << endl;
pointer_char = strtok(NULL, " .!");
string word = pointer_char;
size_t found= str.find(word);
if (found!=string::npos)
cout << "Position of " << word << " found at: " << int(found) << endl;
system("pause");
}
return 0;
The problem is you aren't checking the return value of strtok.
pointer_char = strtok(NULL, " .!");
string word = pointer_char;
You're only testing it at the top of the loop.
pointer_char = strtok(nullptr, " .!");
if (pointer_char == nullptr)
break;
Why does this not work:
SomeClass::SomeClass(char *lit) //Ctor
{
str = new char[strlen(lit)+1]; // str is a pointer to char in SomeClass
strcpy(str,"have");
cout << str << " " << "In Ctor" << " +Size=" << strlen(str)<< endl;
}
The above code shows a string with length 0. But this code works:
SomeClass::SomeClass(char *lit)
{
char newstr[strlen(lit)+1];
strcpy(newstr,"have");
cout << newstr << " " << "In Ctor" << " +Size=" << strlen(newstr)<< endl;
}
Here is the complete code.
EDIT:
Added the link to Ideone which OP removed after I answered the Question.
Without the link to source code, this Q & answer to it is useless.
There is no problem with the strcpy, You are just messing your pointer.
The problem is here:
str = new char[strlen(lit)+1];
strcpy(str,lit);
length=leng(); <------------- str points to \0 after this call
cout << str << " " << "In Ctor" << " +Size=" << strlen(lit)<< endl;
str is your class member and You move the pointer str to point to the \0 in the function leng(), Naturally, You don't see any output in the next statement.
Solution is to hold the starting address in a separate pointer inside the function.
int String :: leng()
{
int length=0;
char *tempPtr= str; <----------- Store the address in a temporary pointer
while(*str)
{
length++;
str++;
}
str = tempPtr; <---------- Point the Pointer member to right address again
return length;
}
Another way to write String::leng():
int String::leng()
{
char *endPtr = str;
while(*endPtr)
endPtr++;
return endPtr - str;
}