How to change the value of string from a cout - string

void extractWord (string& str)
I have to write a function that extracts the word between ‘*’.
For example, using the three test cases below:
string s = "This is to be *reversed*"
string s1 ="*Reversed* starts here";
string s2 = "This is *in* the middle";
and after each function call,
s=reversed, s1=Reversed, s2=in
So i figured out...
void extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
for(int i=0;i<sizeof(str);i++)
{
if ((i>idx1)&&(i<idx2))
cout<<str[i];
}
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
extractWord(s);
cout<<endl;
extractWord(s1);
cout<<endl;
extractWord(s2);
cout<<endl;
}
but how do I change the value of s into the output of this function?

I have modified your code a bit. I hope this solves your problem:
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;
void extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
str=str.substr(idx1+1,idx2-idx1-1); //changed
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
extractWord(s);
cout<<s<<endl; //changed
extractWord(s1);
cout<<s1<<endl; //changed
extractWord(s2);
cout<<s2<<endl; //changed
system("PAUSE");
return 0;
}
Now, every time you call void extractWord (string& str) it replaces string with only word between *'s.
I've used std::string::substr function. > http://www.cplusplus.com/reference/string/string/substr/
Other option is to make function that returns word between *'s.
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;
string extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
return str.substr(idx1+1,idx2-idx1-1);
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
string res;
res=extractWord(s);
cout<<res<<endl;
res=extractWord(s1);
cout<<res<<endl;
res=extractWord(s2);
cout<<res<<endl;
system("PAUSE");
return 0;
}
However, note that it doesn't work if your string do not contain two *'s or it has more than one word that has to be extracted. I hope this helped you.

Related

Identifier "stack" (my class) is undefined even though headers are properly included

In my source file when I define my object s, Visual Studios says Identifier "stack" is undefined. I'm pretty sure I've separated the headers correctly but I don't know why I'm getting this error. Also side note when I put everything together in one source file and compile it, it just exits immediately without any reason. Thanks for the help in advance.
This is the source file
// pa3.cpp : Defines the entry point for the console application.
//
#include "stack.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//#include <ctype.h>
using namespace std;
int main()
{
//int count;
stack s; //assign s object to stack
string input;
cout << "Please enter the name of the input file: \n";
//cin >> input;
getline(cin, input);
ifstream file(input);
string readline;
//ifstream file(input);
while (getline(file, readline)) //take first line of file and assign to readline
{
s.push(readline); //send it off to push
s.retrieveUPPER();
//file.close();
system("pause");
}
return 0;
}
This is the stack.h file
//#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//#include <ctype.h>
using namespace std;
class stack
{
public:
int count;
void push(string);
//void pop();
void check(string);
void retrieveUPPER();
private:
static string firstline[1];
static string diskeywords[3];
static char upperword[100];
static char lowerword[100];
static char operatorsarr[100];
static char delimitersarr[100];
};
This is the stack.cpp
#include "stack.h"
#include "stdafx.h"
#include <iostream>
string stack::firstline[1] = { 0 };
string stack::diskeywords[3];
char stack::upperword[100];
char stack::lowerword[100];
char stack::operatorsarr[100];
char stack::delimitersarr[100];
void stack::retrieveUPPER()
{
for (int i = 0; i < 100; i++)
{
cout << upperword[i] << "\n";
}
}
void stack::push(string readline)
{
firstline[0] = readline;
count++;
check(readline);
}
void stack::check(string readline)
{
int length;
char letter;
int leftperenthe = 0;
int rightperenthe = 0;
//int capital;
//int wordFOR;
//int wordBEGIN;
//char keywords[3][8] = { "FOR", "BEGIN", "END" };
char operators[] = "+-=*/;";
char delimiters[] = { ',',';' };
length = readline.length();
for (int i = 0; i < length; i++)
{
for (int j = 0; j < 5; j++)
{
letter = readline[i];
if (isupper(letter)) //if capital letter
{
upperword[i] = letter;
}
else if (islower(letter)) //if lowercase letter
{
lowerword[i] = letter;
}
else if (letter == operators[j]) //if encounters a operator
{
operatorsarr[i] = letter;
}
else if (letter == delimiters[j]) //if encounters a delimiter
{
delimitersarr[i] = letter;
}
else if (letter = ' ') //if encounters a space
{
lowerword[i] = ' ';
operatorsarr[i] = ' ';
delimitersarr[i] = ' ';
}
else if (letter = '(') //if left perenthesis
{
leftperenthe++;
}
else if (letter = ')') //if right perenthesis
{
rightperenthe++;
}
}
}
}
I also had a similar situation:
#include<iostream>
#include<stack.h>
#include<vector>
using namespace std;
int main();
std::vector<int>stickPan (std::vector<int>arr)
{
stack<int>s;
...
}
I used VScode, used mingw-w64, and added paths for * .h files to .json files, but it didn't work.
Errors are reported:
identifier "stack" is undefined.
type name is not allowed.
identifier "s" is undefined

Arduino: Splitting a string AND randomly selecting tokens

I built a program in Processing that pseudorandomly generates imperative sentences. The program combines a randomly selected verb, possessive adjective, and noun in order to display a final sentence.
Here is a summarized version of the program:
void sentence() {
String VerbList = "abide accelerate accept accomplish achieve acquire acted etc.”;
String[] Verbs = VerbList.split("\\s");
String PossessiveAdjectiveList = "my your his her its our their";
String [] PossessiveAdjectives = PossessiveAdjectiveList.split("\\s");
String NounList = "account achiever acoustics act action activity actor etc.”;
String[] Nouns = NounList.split("\\s");
int verb = int(random(Verbs.length));
int possessiveAdjective = int(random(PossessiveAdjectives.length));
int noun = int(random(Nouns.length));
String Sentence = Verbs[verb]+" "+PossessiveAdjectives[possessiveAdjective]+" "+Nouns[noun];
println(Sentence);
Upon moving the code to the Arduino IDE, I immediately discovered the absence of a string.split function. I understand that I can turn a string into tokens using strtok; however, I am not sure how to select individual tokens through randomly generated integers. Should I be trying to use strtok? Here is my code thus far:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define WHITE 0x7
void setup() {
Serial.begin(9600);
lcd.setBacklight(WHITE);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
sentence();
}
void sentence() {
char VerbList[] = "abide accelerate accept accomplish achieve acquire acted etc.";
char* Verbs = strtok(VerbList, " ");
char PossessiveAdjectiveList[] = "my your his her its our their";
char* PossessiveAdjectives = strtok(PossessiveAdjectiveList, " ");
char NounList[] = "account achiever acoustics act action activity actor etc.";
char* Nouns = strtok(NounList, " ");
//int verb = int(random(Verbs.length));
//int verb = Verbs.substring(random(Verbs.length));
//int possessiveAdjective = int(random(PossessiveAdjectives.length));
//int noun = int(random(Nouns.length));
//String Sentence = Verbs[verb]+" "+PossessiveAdjectives[possessiveAdjective]+" "+Nouns[noun];
//lcd.print(Sentence);
}
uint8_t i=0;
void loop() {
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_SELECT) {
sentence();
}
}
}
After doing much research, I have found a decent answer to my problem.
This code splits the verb, noun, and possessive adjective strings into tokens, and then selects a token from each string based on random integers. These tokens are then added up to form an imperative sentence. The final sentence is displayed on a 16X2 character LCD shield.
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define MAX_STRING_LEN 1000
char *Verbs = "abide accelerate accept accomplish achieve";
char *PossessiveAdjectives = "my your his her its our their";
char *Nouns = "account achiever acoustics act action activity";
char *p, *i;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
int verbCount = 0,vc;
int adjectiveCount = 0,ac;
int nounCount = 0,nc;
for(vc=0;vc<strlen(Verbs);vc++){
if(Verbs[vc] == ' ')
verbCount++;
}
for(ac=0;ac<strlen(PossessiveAdjectives);ac++){
if(PossessiveAdjectives[ac] == ' ')
adjectiveCount++;
}
for(nc=0;nc<strlen(Nouns);nc++){
if(Nouns[nc] == ' ')
nounCount++;
}
int randVerb = random(1,verbCount+2);
int randPossessiveAdjective = random(1,adjectiveCount+2);
int randNoun = random(1,nounCount+2);
String Verb = subStr(Verbs, " ", randVerb);
String PossessiveAdjective = subStr(PossessiveAdjectives, " ", randPossessiveAdjective);
String Noun = subStr(Nouns, " ", randNoun);
String ImperativeSentence = Verb+" "+PossessiveAdjective+" "+Noun;
if(Verb.length()+PossessiveAdjective.length()+Noun.length()+2 > 16) {
if(Verb.length()+PossessiveAdjective.length()+1 > 16) {
if(Verb.length() > 16) {
setup;
}
else {
lcd.print(Verb);
lcd.setCursor(0, 1);
lcd.print(PossessiveAdjective);
lcd.print(" ");
lcd.print(Noun);
}
}
else {
lcd.print(Verb);
lcd.print(" ");
lcd.print(PossessiveAdjective);
lcd.setCursor(0, 1);
lcd.print(Noun);
}
}
else {
lcd.setCursor(0, 0);
lcd.print(ImperativeSentence);
}
Serial.println(ImperativeSentence);
}
uint8_t a=0;
void loop() {
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_SELECT) {
setup();
}
}
}
char* subStr (char* str, char *delim, int index) {
char *act, *sub, *ptr;
static char copy[MAX_STRING_LEN];
int i;
strcpy(copy, str);
for (i = 1, act = copy; i <= index; i++, act = NULL) {
sub = strtok_r(act, delim, &ptr);
if (sub == NULL) break;
}
return sub;
}
This is clearly a work in progress. I still need to store a word bank on an SD card, and further simplify the code. Any suggestions would be greatly appreciated.

C++ vector and string, count vowels

This program supposed to read a text and count the number of vowels and consonants. it should ignore any non alphabetic characters. the result should me something like this:
Enter your text: I have to TURN this..in before midnight!!
a, e, i, o, u, y
1, 3, 5, 2, 1, 0
There are 19 consonants.
but the result from my code is :
Enter your text: I have to TURN this..in before midnight!!
a, e, i, o, u, y
1, 3, 4, 2, 0, 0
There are 31 consonants.
I dont know what is happening!! Also this is an assignment and I have to use all these functions and I cannot add or remove them! I read couple of other ways to count and display the numbers but unfortunately the template was given...
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies);
string read_text(const string & prompt);
bool is_alphabetic(const char character);
void create_list(const string & str_text, vector<char> & vec_text);
bool is_member(const vector<char> & list, char character);
int find_index(const vector<char> & list, char character);
int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs);
void display_characters(const vector<char> & characters, const int colwidth);
void display_freqs(const vector<int> & freqs, const int colwidth);
int main()
{
vector<char> vowels;
vector<int> freqs;
string input;
vector<char> text;
int consonants(0);
const int COLUMNWIDTH = 2;
init_vectors(vowels, freqs);
input=read_text("Enter your text: ");
create_list(input, text);
compute_vowel_freqs(text, vowels, freqs);
display_characters(vowels, COLUMNWIDTH);
display_freqs(freqs, COLUMNWIDTH);
consonants = compute_vowel_freqs(text, vowels, freqs);
cout<<"There are "<< consonants<< " consonants."<<endl;
return 0;
}
void init_vectors(vector<char> & vowels, vector<int> & frequencies)
{
for (int i(0); i<6; i++) //i is loop variable
{
frequencies.push_back(0);
}
vowels.push_back('a');
vowels.push_back('e');
vowels.push_back('i');
vowels.push_back('o');
vowels.push_back('u');
vowels.push_back('y');
}
string read_text(const string & prompt)
{
string phrase;
cout<<prompt;
getline(cin,phrase);
return phrase;
}
bool is_alphabetic(const char character)
{
bool alphabet;
if ((character > 'a' && character < 'z')||(character > 'A' && character < 'Z'))
{
alphabet = true;
}
return alphabet;
}
void create_list(const string & str_text, vector<char> & vec_text)
{
for( int i = 0 ; i < str_text.length() ; i++)
{
if(is_alphabetic(str_text[i]))
vec_text.push_back(str_text[i]);
}
}
bool is_member(const vector<char> & list, char character)
{
bool vowel;
for (int i(0); i<list.size(); i++)
{
if (character == list[i])
{
vowel=true;
}
}
return vowel;
}
int find_index(const vector<char> & list, char character)
{
int index = -1;
for(int i=0; i<list.size(); i++)
{
if(character == list[i])
{
index = i;
break;
}
}
return index;
}
int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs)
{
int num_cons(0);
for(int i = 0 ; i < text.size() ; i++)
{
int index;
if(is_member(vowels, text[i]))
{
index = find_index(vowels , tolower(text[i]));
freqs[index]++;
}
else
num_cons++;
}
return num_cons;
}
void display_characters(const vector<char> & characters, const int colwidth)
{
for(int i=0; i<characters.size(); i++)
{
cout<<setw(colwidth)<<characters[i];
if((i+1)<characters.size())
{
cout<<",";
}
}
cout<<endl;
return;
}
void display_freqs(const vector<int> & freqs, const int colwidth)
{
for(int i=0; i<freqs.size(); i++)
{
cout<<setw(colwidth)<<freqs[i];
if((i+1)<freqs.size())
cout<<",";
}
cout<<endl;
return;
}
If you write hundreds of lines of code before you test any of it, you're bound to fail. Start small and simple, add complexity a little at a time, test at every step, and never add to code that doesn't work.
You should have tested these functions one by one as you wrote them. Here's the first problem:
bool is_alphabetic(const char character)
{
bool alphabet;
if ((character > 'a' && character < 'z')||(character > 'A' && character < 'Z'))
{
alphabet = true;
}
return false;
}
This always returns false, so nothing is recognized as text.
EDIT:
Second problem: is_member has exactly the same bug, with the same solution.
EDIT:
Third problem: I failed to notice that in this line in is_alphabetic:
if ((character > 'a' && character < 'z')||(character > 'A' && character < 'Z'))
You're using '>' and '<' when you should use ">=" and "<=". According to this function, 'a' and 'z' are not letters.
Look, you're still trying to test and fix this program as a whole. You must test it piecemeal. Pick a place in main and print out every variable that should have been assigned a value by then. This function may be useful:
void printVector(const vector<char> &V)
{
for(vector<char>::const_iterator citr=V.begin(); citr!=V.end(); ++citr)
cout << *citr;
cout << endl;
}
Then inspect the results. If a variable doesn't contain what it should, then something above that point is misbehaving. Trace the problem back to a function where good things go in but something bad comes out. Fix that, then look at the output again. I can't emphasize this enough: don't try to fix everything at once.
Your is_member is always false, so no char is vowel. And it results in 0 count.
bool is_member(const vector<char> & list, char character)
{
bool vowel = false; # this fix is not obligatory, I just made code look clearer
for (int i(0); i<list.size(); i++)
{
if (character == list[i])
{
vowel=true;
}
}
return vowel; # this should be fixed
}
By the way, the same issue is in the function 'is_alphabetic'. It is always false.

Good way to tokenize line from file without using external libraries?

I am trying to tokenize a database dump separated by commas. I only need to read the first word, which will tell me if this is the line I need and then tokenize the line and save each separated string in a vector.
I have had trouble keeping all of the datatypes in order. I use a method of getline:
string line;
vector<string> tokens;
// Iterate through each line of the file
while( getline( file, line ) )
{
// Here is where i want to tokenize. strtok however uses a character array and not a string.
}
The thing is, I only want to continue reading and tokenize a line if the first word is what I am after. Here is a sample of a line from the file:
example,1,200,200,220,10,550,550,550,0,100,0,-84,255
So, if I am after the string example, it goes ahead and tokenizes the rest of the line for my use and then stops reading from the file.
Should I be using strtok, stringstream or something else?
Thank you!
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void do(ifstream& file) {
string line;
string prefix = "example,";
// Get all lines from the file
while (getline(file,line).good()) {
// Compare the beginning for your prefix
if (line.compare(0, prefix.size(), prefix) == 0) {
// Homemade tokenization
vector<string> tokens;
int oldpos = 0;
int pos;
while ((pos = line.find(',', oldpos)) != string::npos) {
tokens.push_back(line.substr(oldpos, pos-oldpos));
oldpos = pos + 1;
}
tokens.push_back(line.substr(oldpos)); // don't forget the last bit
// And here you are!
}
}
}
How do I tokenize a string in C++?
http://www.daniweb.com/software-development/cpp/threads/27905
Hope this helps, though I am not proficient C/C++ programmer. For the record it would be nice if you could specify in the tags or in post language you are using.
Tokenizer.h
#ifndef TOKENIZER_H
#define TOKENIZER_H
#include <string>
#include <vector>
#include <sstream>
class Tokenizer
{
public:
Tokenizer();
~Tokenizer();
void Tokenize(std::string& str, std::vector<std::string>& tokens);
};
#endif /* TOKENIZER_H */
Tokenizer.cpp
#include "Tokenizer.h"
using namespace std;
string seps(string& s) {
if (!s.size()) return "";
stringstream ss;
ss << s[0];
for (int i = 1; i < s.size(); i++)
ss << '|' << s[i];
return ss.str();
}
void tok(string& str, vector<string>& tokens, const string& delimiters = ",")
{
seps(str);
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
}
Tokenizer::Tokenizer()
{
}
void Tokenizer::Tokenize(string& str, vector<string>& tokens)
{
tok(seps(str), tokens);
}
Tokenizer::~Tokenizer()
{
}
To tokenize a string
#include "Tokenizer.h"
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// Required variables for later below
vector<string> t;
string s = "This is one string,This is another,And this is another one aswell.";
// What you need to include:
Tokenizer tokenizer;
tokenizer.Tokenize(s, t); // s = a string to tokenize, t = vector to store tokens
// Below is just to show the tokens in the vector<string> (c++11+)
for (auto c : t)
cout << c << endl;
system("pause");
return 0;
}

Finding a substring of multiple occurences in a string [C++]

is there any STL algorithm or a standard way of finding how many occurences of particular substring are there in a string? For example in string:
'How do you do at ou'
the string "ou" appears twice. I tried some STL algorithms with and without predicates but what I found is that those algorithms from STL want to compare components of string which in my case is char but cannot? compare substrings.
I come up with something like this:
str - string
obj - substring we're looking for
std::string::size_type count_subs(const std::string& str, const std::string& obj)
{
std::string::const_iterator beg = str.begin();
std::string::const_iterator end = str.end();
std::string::size_type count = 0;
while ((beg + (obj.size() - 1)) != end)
{
std::string tmp(beg, beg + obj.size());
if (tmp == obj)
{
++count;
}
++beg;
}
return count;
}
thank you.
#include <string>
#include <iostream>
int Count( const std::string & str,
const std::string & obj ) {
int n = 0;
std::string ::size_type pos = 0;
while( (pos = obj.find( str, pos ))
!= std::string::npos ) {
n++;
pos += str.size();
}
return n;
}
int main() {
std::string s = "How do you do at ou";
int n = Count( "ou", s );
std::cout << n << std::endl;
}

Resources