I have written the following code to save in an char * array and print the following content:
band1.txt
band2.txt
...
band3.txt
The code seems right but what is printed on the console is very weird.
Code:
const char ** current_band = new const char * [103];
stringstream sstm;
string str;
for (i=0;i<103;i++){
current_band[i] = new char[11];
}
for (i=0;i<103;i++){
sstm.str("");
sstm << "band" << i+1 << ".txt";
str = sstm.str();
current_band[i] = str.c_str();
cout << current_band[i] << endl;
cout << i << endl;
}
for (i=0;i<103;i++){
cout << current_band[i] << endl;
cout << i << endl;
}
Console:
band1.txt
0
band2.txt
1
...
band103.txt
102
And then for the last loop:
band103.txt
0
band102.txt
1
band103.txt
2
band102.txt
3
...
band102.txt
101
band103.txt
102
How is this even possible?
EDIT: Actually i want the "bands" to be char* in order to call the ifstream current_band_file(current_band) constructor that wants such an argument
You have undefined behavior by using pointers to already destroyed objects.
Simply don't use raw pointers and raw arrays and such stuff yet.
std::string is your friend for strings, std::vector is your friend for arrays.
Example:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
auto main()
-> int
{
vector<string> band_names;
for( int i = 1; i <= 103; ++i )
{
band_names.push_back( "band" + to_string( i ) );
}
for( string const& name : band_names )
{
cout << name << endl;
}
}
As a minimal change to you existing code you can change:
current_band[i] = str.c_str();
to:
strcpy(current_band[i], str.c_str());
However, moving away from this mixed C and C++ to more idiomatic C++ (like Cheers and hth. - Alf's answer) will serve you better for the future.
Sticking with things like char[11] over std::string means you're stuck with:
The arbitrary choice of max length 11 even though probably there is no good technical reason for that limit.
Dealing with handling all the details of memory allocation which a proper C++ implementation hides.
The much less natural to read lower level code style.
As a band-aid you could replace:
current_band[i] = str.c_str();
with
if ( str.size() >= 11 )
throw std::runtime_error("string too long");
std::strcpy(current_band[i], str.c_str());
However it would be a much better idea to replace this whole thing with:
std::vector<std::string> current_band(103);
int i = 0;
for (auto &s : current_band)
{
// your sstm stuff, storing to s
}
Here's an alternative way that's a little more robust, readable and more likely to be correct.
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
vector<string> bands;
bands.reserve(103);
for(size_t i = 1 ; i <= 103 ; ++i) {
ostringstream ss;
ss << "band" << i;
bands.emplace_back( ss.str() );
}
for (size_t index = 0 ; index < bands.size() ; ++index) {
cout << index << " : " << bands[index] << endl;
}
return 0;
}
output:
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
Executing the program....
$demo
0 : band1
1 : band2
2 : band3
...
100 : band101
101 : band102
102 : band103
Related
string a = MwZwXxZwDwJrBxHrHxMrGrJrGwHxMrFrZrZrDrKwZxLrZrFwZxErMrXxArZw;
Assume i have this data in my string . I want to record how many M , Z , X , D , J (including those capital letters i didn't mentions ) in in string how can do it ? My friends say use vector can do it but i does not really know how to use vector is there any alternative way to do it .
I tried using for loops to do and find the M , and reset the pointer to 0 to continue find the next capital value , but not sure is there any easier way to do it .
first I'll show you a 'easier' way to me.
#include <iostream>
#include <map>
using namespace std;
int main(int argc, const char * argv[]) {
string str = "MwZwXxZwDwJrBxHrHxMrGrJrGwHxMrFrZrZrDrKwZxLrZrFwZxErMrXxArZw";
map<char,int> map;
for (int i=0; i<str.length(); i++) {
char ch = str[i];
if (isupper(ch)) {
map[ch] ++;
}
}
for (auto item : map) {
cout<<item.first<<':'<<item.second<<endl;
}
return 0;
}
you'll only need to use 1 loop to solve your problem.
the 'isupper(int _c)' is a function from the standard library, it can tell you wether a character is a capital letter.
the 'map' is a data structure from the standard library too, it can do key-value storage for you.
this program outputs this:
A:1
B:1
D:2
E:1
F:2
G:2
H:3
J:2
K:1
L:1
M:4
X:2
Z:8
is this what you want?
Use regex.
using namespace std;
// regex_search example
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("MwZwXxZwDwJrBxHrHxMrGrJrGwHxMrFrZrZrDrKwZxLrZrFwZxErMrXxArZw;");
std::smatch m;
std::regex e ("[A-Z\s]+");
map<string,int> map;
std::cout << "Target sequence: " << s << std::endl;
std::cout << "Regular expression: [A-Z\s]+" << std::endl;
std::cout << "The following matches and submatches were found:" << std::endl;
while (std::regex_search (s,m,e)) {
for (auto x:m)
{
//cout << x << " ";
map[x.str()] ++;
}
//cout << std::endl;
s = m.suffix().str();
}
for (auto item : map) {
cout<<item.first<<':'<<item.second<<endl;
}
return 0;
}
The most direct translation of "loop through the string and count the uppercase letters" into C++ I can think of:
#include <iostream>
#include <map>
#include <cctype>
int main()
{
string a = "MwZwXxZwDwJrBxHrHxMrGrJrGwHxMrFrZrZrDrKwZxLrZrFwZxErMrXxArZw";
std::map<char, int> count;
// Loop through the string...
for (auto c: a)
{
// ... and count the uppercase letters.
if (std::isupper(c))
{
count[c] += 1;
}
}
// Show the result.
for (auto it: count)
{
std::cout << it.first << ": " << it.second << std::endl;
}
}
Here's my code:
#include <iostream>
using namespace std;
string moveString(string t, int index)
{
for (int i=index; t[i]!=NULL;i++)
{
t[i]=t[i+1];
}
return t;
}
string delChars(string t)
{
for (int i=0; t[i]!=NULL; i++)
{
if (t[i]>'a' && t[i]<'z')
{
moveString(t, i);
}
else if (t[i]>'A' && t[i]<'Z')
{
moveString(t, i);
}
}
return t;
}
int main()
{
int numberOfSpaces;
string t;
cout << "Text some word: "; cin>>t;
cout<<delChars(t);
return 0;
}
First function moveString should (in theory) take down every single character from a string by 1 index down (starting from given index) - to remove 1 character. The rest is pretty obvious. But:
Input: abc123def
Output: abc123def
What am I doing wrong?
And a additional mini-question: Acutally, what's the best way to "delete" an element from an array? (array of ints, chars, etc.)
Logic Stuff is right but his answer is not enough. You shouldn't increase i after move. Since the i.th character is removed and i points to the next character now.
string delChars(string t)
{
for (int i=0; t[i]!=NULL; )
{
if (t[i]>'a' && t[i]<'z')
{
t = moveString(t, i);
}
else if (t[i]>'A' && t[i]<'Z')
{
t = moveString(t, i);
}
else
i++;
}
return t;
}
moveString takes t by value and you're not assigning its return value, so it doesn't change t in delChars. So, make sure the next thing you learn are references.
Apart from that, I don't know what to tell about t[i] != NULL (if it is undefined behavior or not), but we have std::string::size to get the length of std::string, e.g. i < t.size(). And if you havet[i + 1], the condition should then be i + 1 < t.size().
Whatever, don't play with it like with char arrays, leaving the string with previous size. You can pop_back the last (duplicate) character after shifting the characters.
It's worth mentioning that it can be done in one line of idiomatic C++ algorithms, but you want to get your code working...
What am I doing wrong?
Not using standard algorithms
Actually, what's the best way to "delete" an element from array? (array of ints, chars, etc.)
By using the standard remove-erase idiom:
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstring>
int main()
{
using namespace std;
auto s = "!the 54 quick brown foxes jump over the 21 dogs."s;
cout << "before: " << quoted(s) << endl;
s.erase(std::remove_if(s.begin(),
s.end(),
[](auto c) { return std::isalpha(c); }),
s.end());
cout << "after: " << quoted(s) << endl;
return 0;
}
expected output:
before: "!the 54 quick brown foxes jump over the 21 dogs."
after: "! 54 21 ."
I'm not allowed to use standard algorithms
Then keep it simple:
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstring>
std::string remove_letters(const std::string& input)
{
std::string result;
result.reserve(input.size());
for (auto c : input) {
if (!std::isalpha(c)) {
result.push_back(c);
}
}
return result;
}
int main()
{
using namespace std;
auto s = "!the 54 quick brown foxes jump over the 21 dogs."s;
cout << "before: " << quoted(s) << endl;
auto s2 = remove_letters(s);
cout << "after: " << quoted(s2) << endl;
return 0;
}
I am brand new to programming so none of this may be right. I was just messing around trying to get the effect that I (finally) achieved. More for practice than anything else, but I wondered if there was another way to do it.
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1="Don't even think about it!";
string str2;
string str3;
string str4 = "";
for (int i = 0; i < str1.length(); i++){
str2 = str1.substr (0, str1.length()-i);
cout << str2 << str4;
for (int x = str2.length() - 1; x >= 0; x--){
str3 = str1[x];
cout << str3;
}
str4 = str4 + " ";
cout << "\n";
}
getch();
main();
return 0;
}
The question I have is this: Is there a way to make the str3, after it is backwards, its own string that I could then justify right instead of adding spaces with str4?
In order to get a reversed string, just pass the reverse-iterators to the constructor of a new string:
#include <iostream>
#include <string>
int main() {
std::string s = "this is a test";
std::string s_reversed(s.rbegin(), s.rend());
std::cout << s << "\n" << s_reversed << "\n";
}
The output is:
$ g++ test.cc && ./a.out
this is a test
tset a si siht
Applied to your problem:
#include <iostream>
#include <string>
int main() {
std::string s = "this is a test";
for (auto i = s.length(); i > 0; i--) {
std::cout << s;
std::cout << std::string(s.rbegin(), s.rend());
std::cout << '\n';
s[i-1] = ' ';
}
}
Note that there also is an algorithm in the standard library to reverse a container, e.g., a string: std::reverse().
For reference:
http://en.cppreference.com/w/cpp/string/basic_string/basic_string
http://en.cppreference.com/w/cpp/string/basic_string/rbegin
http://en.cppreference.com/w/cpp/string/basic_string/rend
http://en.cppreference.com/w/cpp/algorithm/reverse
You could try this:
replacing letters for whitespace and then reversing the string.
Concat the modified string with the reverse of it and print it.
std::string reverse(std::string str)
{
std::reverse(str.begin(), str.end());
return str;
}
int main()
{
std::string str = "Don't even think about it!";
size_t N = str.length();
for (size_t i = 1; i < N; i++)
{
std::cout << str << reverse(str) << std::endl;
str.replace(N-i,1, " ");
}
return 0;
}
First I would rename the variables so it is more clear what you are trying to achieve. Believe me, in two months you will not have a clue what this code actually does. Suggestion:
str1 -> baseString
str2 -> forwardsClippedString
str3 -> backwardsClippedString
str4 -> gapString
Maybe these names are not even good but I think they are better than str1, ..., str4.
Then I would make a separate method of the inner loop. After that make a separate method of the outer for loop - yes, main() shouldn't be complicated at all.
Finally instead of recursively calling main I suggest to do a while-loop. An infinite one if you please (but it would be nice if one key quits the loop then).
I wrote a C++ program that reads a text file. I want the program to count the number of times a word appears, however. For example, the output should look as follows:
Word Frequency Analysis
Word Frequency
I 1
don't 1
know 1
the 2
key 1
to 3
success 1
but 1
key 1
failure 1
is 1
trying 1
please 1
everybody 1
Notice how each word appears only once. What do I need to do in order to achieve this effect??
Here is the text file (i.e. named BillCosby.txt):
I don't know the key to success, but the key to failure is trying to please everybody.
Here is my code so far. I am having an extreme mental block and cannot figure out a way to get the program to read the number of times a word occurs.
#include <iostream>
#include <fstream>
#include <iomanip>
const int BUFFER_LENGTH = 256;
const int NUMBER_OF_STRINGS = 100;
int numberOfElements = 0;
char buffer[NUMBER_OF_STRINGS][BUFFER_LENGTH];
char * words = buffer[0];
int frequency[NUMBER_OF_STRINGS];
int StringLength(char * buffer);
int StringCompare(char * firstString, char * secondString);
int main(){
int isFound = 1;
int count = 1;
std::ifstream input("BillCosby.txt");
if(input.is_open())
{
//Priming read
input >> buffer[numberOfElements];
frequency[numberOfElements] = 1;
while(!input.eof())
{
numberOfElements++;
input >> buffer[numberOfElements];
for(int i = 0; i < numberOfElements; i++){
isFound = StringCompare(buffer[numberOfElements], buffer[i]);
if(isFound == 0)
++count;
}
frequency[numberOfElements] = count;
//frequency[numberOfElements] = 1;
count = 1;
isFound = 1;
}
numberOfElements++;
}
else
std::cout << "File is not open. " << std::endl;
std::cout << "\n\nWord Frequency Analysis " << std::endl;
std::cout << "\n" << std::endl;
std::cout << "Word " << std::setw(25) << "Frequency\n" << std::endl;
for(int i = 0; i < numberOfElements; i++){
int length = StringLength(buffer[i]);
std::cout << buffer[i] << std::setw(25 - length) << frequency[i] <<
std::endl;
}
return 0;
}
int StringLength(char * buffer){
char * characterPointer = buffer;
while(*characterPointer != '\0'){
characterPointer++;
}
return characterPointer - buffer;
}
int StringCompare(char * firstString, char * secondString)
{
while ((*firstString == *secondString || (*firstString == *secondString - 32) ||
(*firstString - 32 == *secondString)) && (*firstString != '\0'))
{
firstString++;
secondString++;
}
if (*firstString > *secondString)
return 1;
else if (*firstString < *secondString)
return -1;
return 0;
}
Your program is quite confusing to read. But this part stuck out to me:
frequency[numberOfElements] = 1;
(in the while loop). You realize that you are always setting the frequency to 1 no matter how many times the word appears right? Maybe you meant to increment the value and not set it to 1?
One approach is to tokenize (split the lines into words), and then use c++ map container. The map would have the word as a key, and word count for value.
For each token, add it into the map, and increment the wordcount. A map key is unique, hence you wouldn't have duplicates.
You can use stringstream for your tokenizer, and you can find the map container reference (incl examples) here.
And don't worry, a good programmer deals with mental blocks on a daily basis -- so get used to it :)
Flow of solution should be something like this:
- initialize storage (you know you have a pretty small file apparently?)
- set initial count to zero (not one)
- read words into array. When you get a new word, see if you already have it; if so, add one to the count at that location; if not, add it to the list of words ("hey - a new word!") and set its count to 1
- loop over all words in the file
Be careful with white space - make sure you are matching only non white space characters. Right now you have "key" twice. I suspect that is a mistake?
Good luck.
Here's a code example that I tested with codepad.org:
#include <iostream>
#include <map>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string s = "I don't know the key to success, but the key to failure is trying to please everybody.";
string word;
map<string,int> freq;
for ( std::string::iterator it=s.begin(); it!=s.end(); ++it)
{
if(*it == ' ')
{
if(freq.find(word) == freq.end()) //First time the word is seen
{
freq[word] = 1;
}
else //The word has been seen before
{
freq[word]++;
}
word = "";
}
else
{
word.push_back(*it);
}
}
for (std::map<string,int>::iterator it=freq.begin(); it!=freq.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
}
It stops when it finds a space so grammatical symbols will mess things up but you get the point.
Output:
I => 1
but => 1
don't => 1
failure => 1
is => 1
key => 2
know => 1
please => 1
success, => 1 //Note this isn't perfect because of the comma. A quick change can fix this though, I'll let //you figure that out on your own.
the => 2
to => 3
trying => 1
I'm a bit hesitant to post a direct answer to something that looks a lot like homework, but I'm pretty sure if somebody turns this in as homework, any halfway decent teacher/professor is going to demand some pretty serious explanation, so if you do so, you'd better study it carefully and be ready for some serious questions about what all the parts are and how they work.
#include <map>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <iomanip>
#include <locale>
#include <vector>
struct alpha_only: std::ctype<char> {
alpha_only() : std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table() {
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
for (int i=0; i<std::ctype<char>::table_size; i++)
if (isalpha(i)) rc[i] = std::ctype_base::alpha;
return &rc[0];
}
};
typedef std::pair<std::string, unsigned> count;
namespace std {
std::ostream &operator<<(std::ostream &os, ::count const &c) {
return os << std::left << std::setw(25) << c.first
<< std::setw(10) << c.second;
}
}
int main() {
std::ifstream input("billcosby.txt");
input.imbue(std::locale(std::locale(), new alpha_only()));
std::map<std::string, unsigned> words;
std::for_each(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
[&words](std::string const &w) { ++words[w]; });
std::copy(words.begin(), words.end(),
std::ostream_iterator<count>(std::cout, "\n"));
return 0;
}
I have any sequence (or sentence) and i want to extract the last 2 strings.
For example,
sdfsdfds sdfs dfsd fgsd 3 dsfds should produce: 3 dsfds
sdfsd (dfgdg)gfdg fg 6 gg should produce: 6 gg
You can use std::string::find_last_of function to find spaces.
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
size_t found1 = test.find_last_of( " " );
if ( found1 != string::npos ) {
size_t found2 = test.find_last_of( " ", found1-1 );
if ( found2 != string::npos )
std::cout << test.substr(found2+1, found1-found2-1) << std::endl;
std::cout << test.substr(found1+1) << std::endl;
}
return 0;
}
The following will work if your strings are whitespace separated.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string str = "jfdf fhfeif shfowejef dhfojfe";
stringstream sstr(str);
vector<string> vstr;
while(sstr >> str)
{
vstr.push_back(str);
}
if (vstr.size() >= 2)
cout << vstr[vstr.size()-2] << ' ';
if (vstr.size())
cout << vstr[vstr.size()-1] << endl;
return 0;
}
Returns the strings in the wrong order, but if that doesn't matter,
std::string s ("some words here");
std::string::size_type j;
for(int i=0; i<2; ++i) {
if((j = s.find_last_of(' ')) == std::string::npos) {
// there aren't two strings, throw, return, or do something else
return 0;
}
std::cout << s.c_str()+j+1;
s = " " + s.substr(0,j);
}
Alternatively,
struct extract_two_words {
friend std::istream& operator>> (std::istream& in , extract_two_words& etw);
std::string word1;
std::string word2;
};
std::istream& operator>> (std::istream& in , extract_two_words& etw) {
std::string str1, str2;
while(in) {
in >> str1;
in >> str2;
}
etw.word2 = str1;
etw.word1 = str2;
}
I would encourage you to have a look at the Boost library. It has algorithms and data structures that help you tremendously. Here's how to solve your problem using Boost.StringAlgo:
#include <boost/algorithm/string/split.hpp>
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
std::vector<std::string> v;
boost::algorithm::split(v, test, [](char c) { return c==' ';});
std::cout << "Second to last: " << v.at(v.size()-2) << std::endl;
std::cout << "Last: " << v.at(v.size()-1) << std::endl;
}
I would also encourage you to always use the vector::at method instead of []. This will give you proper error handling.
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
size_t pos = test.length();
for (int i=0; i < 2; i++)
pos = test.find_last_of(" ", pos-1);
std::cout << test.substr(pos+1) << std::endl;
}
Simpler :)