Help me please !!
it doesn't work and i didn't founf the mistake !?
there is a function to verify existence of strings in each other ?
Thank You !!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mot1="abc";
string mot2="oooooabcooo";
int j=1;
while((j!=mot1.length())||(j!=0))
{
for(int i=0;i<=mot.length();i++)
{
if(mot1[i]==mot2[i])
{j++;}
else j=0;
}
}
if(j==mot1.length())
cout<<mot1<<" existe dans "<<mot2<<endl;
else
if(j=0)
cout<<"erreur";
return 0;
}
Here is the function you are looking for.
mot2.find(mot1); // also std::find
Hope this helps.
There are out of box tools for this, but if you look for a labor coding, I understand nothing from your code. Instead, I find it this way:
#include <iostream>
#include <string>
using namespace std;
int find(string mot1,string mot2)
{
bool found;
for(int j=0;j<mot2.length()-mot1.length()+1;j++)
{
found=true;
cout<<"comparing "<<mot1.c_str()<<" and "<<(mot2.c_str()+j)<<endl;
for(int i=0;i<mot1.length();i++)
if(mot1[i]!=mot2[i+j])
{
found=false;
break;
}
if(found)
return j;
}
return -1;
}
int main()
{
string mot1="abc";
string mot2="oooooabcooo";
cout<<find(mot1,mot2)<<endl;
return 0;
}
Related
phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isdigit), phrase.end());
In the above code, why do I have to use :: even though I used using namespace std?
#include "Palindrome.h"
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
Palindrome::Palindrome (string Phrase){
phrase=Phrase;
}
void Palindrome::removeNonLetters()
{
phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isdigit), phrase.end());
phrase.erase(remove_if (phrase.begin(), phrase.end(), ::ispunct), phrase.end());
phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isspace), phrase.end());
}
void Palindrome::lowerCase()
{
for (int i=0; i<phrase.length(); i++)
{
phrase[i] = tolower(phrase[i]);
}
}
bool Palindrome::isPalindrome()
{
int length=phrase.length();
int a=0;
for (int i=0;i<length/2;i++)
{
if(phrase[i] != phrase[length-a-1])
{
return false;
break;
}
a++;
}
return true;
}
The above code is to check if the string is a Palindrome. I don't understand why I need to use the first part which is
Palindrome::Palindrome (string Phrase){
phrase=Phrase;
}
I will always get "yes" if I deleted the above part.
The test code in main is
if(test.Palindrome::isPalindrome() == 1){
cout<<"Yes"<<endl;
}
else {
cout<<"No"<<endl;
}
One more question. I try to change the lowercase of above code, I got error. Do anyone know what happen with it? The new code was from https://www.geeksforgeeks.org/conversion-whole-string-uppercase-lowercase-using-stl-c/
Before
void Palindrome::lowerCase()
{
for (int i=0; i<phrase.length(); i++)
{
phrase[i] = tolower(phrase[i]);
}
}
After
void Palindrome::lowerCase(){
transform(phrase.begin(), phrase.end(), phrase.begin, ::tolower);
}
Can anyone explain it to me? Many thanks!
There are multiple isdigit, ispunct, and isspace functions - ones in the global namespace in the <ctype.h> header, and several in the std namespace in the <cctype> and <clocale> headers. Prefixing them with :: says you want to use the ones from the global namespace.
You need to use <string> instead of <string.h> in order to use the std::string class.
Assuming test is a Palindrome object, then test.Palindrome::isPalindrome() should be just test.isPalindrome().
If you omit the Palindrome constructor, then the phrase member remains blank, and your isPalindrome() implementation returns true for a blank phrase (length is 0) because there is nothing for the for loop to check. Which is technically correct - a blank string is a palindrome.
The :: indicates that you're using isdigit and the others from the global namespace. The isdigit is a part of other header files, such as <ctype.h>.
I am having what seems to be a common issue however reading through the replies to the similar questions I can't find the solution to my issue at all as I have already done what they are suggesting such as making the variable an array. I have the following code:
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <future>
using namespace std;
string eng2Str[4] = { "money", "politics", "RT", "#"};
int resArr[4];
int main()
{
engine2(eng2Str[4], resArr[4]);
system("Pause");
system("cls");
return 0;
}
void engine2(string &eng2Str, int &resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++) {
while (getline(fin, line)) {
if (line.find(eng2Str[i]) != string::npos) {
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
Before you mark as duplicate I have made sure of the following:
The array and variable I am trying to assign are both int
Its an array
The error is:
expression must have pointer-to-object type
The error is occurring at the "resArr[i] = fcount;" line and am not sure why as resArr is an int array and I am trying to assign it a value from another int variable. I am quite new to C++ so any help would be great as I am really stuck!
Thanks!
The problem is that you've declared your function to take a reference to a single string and int, not arrays. It should be:
void engine2(string *eng2Str, int *resArr)
or:
void engine2(string eng2Str[], int resArr[])
Then when you call it, you can give the array names as arguments:
engine2(eng2Str, resArr);
Another problem is the while loop in the function. This will read the entire file during the first iteration of the for() loop. Other iterations will not have anything to read, since it will be at the end of the file already. You could seek back to the beginning of the file, but a better way would be to rearrange the two loops so you just need to read the file once.
while (getline(fin, line)) {
for (int i = 0; i < 4; i++) {
if (line.find(eng2Str[i]) != string::npos) {
resArr[i]++;
}
}
}
I would suggest to use std::vector instead of pure C array.
In your code, there are more issues.
You are passing the fourth element of both arrays to the engine2 function.
From your definition of void engine2(string &eng2Str, int &resArr) you expect reference to a string (not array / vector) and an address / reference of int - you need to pass an pointer to the first element of resArr.
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <future>
using namespace std;
vector<string> eng2Str = { "money", "politics", "RT", "#" };
int resArr[4] = {};
void engine2(const vector<string>& eng2Str, int* resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++)
{
while (getline(fin, line))
{
if (line.find(eng2Str[i]) != string::npos)
{
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
int main()
{
engine2(eng2Str, resArr);
system("Pause");
system("cls");
return 0;
}
I am trying to read a string line for line down a .txt file in order to initiate an array of objects using a constructor that takes a string.
The text file is written like
TransAm
Mustang
Corvette
I feel like my loop is not iterating the information I want to be set correctly. Is there an easy way of accomplishing this?
main.cc
#include <string>
#include <iostream>
#include "Car.cc"
#include <fstream>
using namespace std;
int main()
{
Car cars[3];
string STRING;
ifstream infile;
infile.open("cars.txt");
// THIS IS HOW IT'S ACHIEVED USING FOR-LOOP - Sam
for(int i = 0; i<3 && infile;++i){
getline(infile,STRING);
cars[i].setName(STRING);
}
/* THIS IS WHAT I HAD
while(!infile)
{
getline(infile,STRING);
for(int i = 0; i<sizeof(cars);i++){
cars[i].setName(STRING);
}
}
*/
infile.close();
for(int j = 0;j<sizeof(cars);j++){
cars[j].print();
}
}
Car.h
#include <string>
using namespace std;
class Car{
public:
Car();
Car(string);
string getName();
void setName(string);
void print();
private:
string name;
};
Car.cc
#include <string>
#include "Car.h"
using namespace std;
Car::Car()
{
}
Car::Car(string s)
{
setName(s);
}
void Car::setName(string s)
{
name = s;
}
string Car::getName()
{
return name;
}
void Car::print()
{
cout << name;
}
These points need to be corrected:
while (!infile) prevents you from entering the loop.
You don't need two loops.
You can modify your loop like this:
for (int i = 0; i < sizeof(cars) && getline(infile, STRING); ++i)
cars[i].setName(STRING);
Or like this:
for (int i = 0; i < sizeof(cars) && infile; ++i) {
getline(infile, STRING);
cars[i].setName(STRING);
}
Your loop does at the moment nothing if the file is correctly opened. It will only enter if the call to open was unsuccessful.
Change your loop to either
while (getline(infile,STRING))
{
//...
}
or
while (infile)
{
//...
}
As it's been said, "Change while(!infile) to while(getline(infile,STRING))" but do not forget to remove the getline(infile,STRING); afterwards.
My program calculates the length of each word before space and compares it with a fixed number.
The number is chosen from another string (pi).
I don't know why but my variable FLAG is always set to false so I always get the same output.
I don't know where the problem is. Please help out
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
int main() {
int t=0,num;
int i,j,len,space;
bool FLAG;
string pi ="31415926535897932384626433833",song;
cin>>t;
while(t--){
len=0,space=0,i=0,j=0,num=0,FLAG=true;
cin.ignore();
getline(cin,song);
// problem from here
while(1) {
i=0,num=0,FLAG=true;
len=song.length();
space=song.find(' ');
if(space==-1){
if(len==pi[j]){
FLAG=true;
break;
}
else{
FLAG=false;
break;
}
}
else{
while(i<space){
num++;
i++;
}
if(num==pi[j]){
FLAG=true;
j++;
num=0;
i=0;
song.erase(0,space+1);
cout<<song<<endl;
}
else{
FLAG=false;
break;
}
}
}
// to here
if(FLAG==true){
cout<<"It's a pi song."<<"\n";
}
else{
cout<<"It's not a pi song."<<"\n";
}
}
return 0;
}
You are comparing an integer with a character value. i.e. you are comparing 3 with '3'. To get a number from a character digit, subtract '0'.
So you could write
if (len==(pi[j] - '0'))
Also, please learn to use a debugger, you can step through your code to find the line that doesn't work.
I have written a code to copy from first string's element to second string except space.it simply takes input and if it gets a space then it doesn't insert character of first string into second string. when i am printing second string at the last,the string is partially broken up. But instead of space,if i put any character the second string fully prints out.I am trying but could you fix my bug please?
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
char str1[100];
while(cin>>str1)
{
char str2[100];
int k=0;
for(int i=0; str1[i]!='\0'; i++)
{
if(str1[i]!=' ')
{
str2[k] = str1[i];
k++;
}
}
str2[k] = '\0';
cout<<"result is "<<str2<<endl;
}
return 0;
}
You can use gets() and puts() to read/display a string:
#include <iostream>
#include <string.h>
using namespace std;
int main(void)
{
char s1[100], s2[100];
int k=0;
puts("Insert your string:");
gets(s1);
for (int i=0; i<strlen(s1); i++) {
if (s1[i] != ' ') {
s2[k]=s1[i];
k++;
}
}
puts(s2);
}