Check if a string is palindrome
I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word.
I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it.
I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
std::string line;
std::ifstream infile("wordlist.txt");
}
string reverse(string line){
if (line == string(line.rbegin(), line.rend())) {
cout << string;
}
}
All help is appreciated
I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.
You look not to know how to read file contents in C++.
Here's a link of how to do it:
Read file-contents into a string in C++
I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:
https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions
#include<iostream>
#include<algorithm>
#include<string.h>
#include<fstream>
using namespace std;
int main() {
string line="", line_rev="";
ifstream infile;
infile.open("wordlist.txt");
do{
infile>>line;
line_rev=line;
reverse(line_rev.begin(), line_rev.end());
if(line==line_rev)
cout<<line<<endl;
}while(getline(infile, line));
//if(infile.is_open()){cout<<"open"<<endl;} //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}
This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.
Related
Im using string library but I can't seem to use the get line function
used both string and string.h but its still not working
I've added the code below and it basically is just to use input numbers in a text file and then using them in a sorting mehthod
the problem is that im only getting the error mentioned below and can't seem to wrap my head around the solution to it
,,assignmentDTS.cpp:34:17: error: no matching function for call to 'getline'
getline(inputFile,tempnumstring);
#include<iostream>
#include<fstream>
#include<string.h>
#include<time.h>
using namespace std;
class sorting {
public:
void bubblesort(int arraySize){
int num;
int *arr=new int[arraySize];
ofstream inputFile("data.txt");
if(inputFile.is_open()){
for (int i = 0; i <arraySize; i++){
string tempnumstring;
std::getline(inputFile,tempnumstring);
num=stoi(tempnumstring);
arr[i]=num;
}
And there we go: ofstream inputFile("data.txt"); needs to be ifstream inputFile("data.txt"); The fstream starts with i for input stream, rather than o for output stream.
Answered on chat
std::getline is declared in #include <string> per the C++ Library Standard.
The #include <string.h> statement includes the C Library Standard string library which has no getline function. This header contains functions like memcpy and strcpy.
The C++ recommended way to reference the C "string.h" header is #include <cstring>.
I need to make a program in witch I have to read a text from an input file(ifstream fin("input.in")) and store it until the program meets the "#" character. I know it should be doable using fin.getline, but I can't make the "delim" parameter work. I would find useful an explanation of how doe it work, and an example. I already read this, but I couldn't find an example with fin.getline.
This is what I tried, but it doesn't work:
#include <fstream>
#include <string.h>
#include <string>
using namespace std;
ifstream fin("cod.in");
ofstream fout("cod.out");
char chr[100];
for(int i=0;i<n;i++)
{
fin.getline(chr,'#');
fout<<chr<<" ";
}
I keep getting this error (it's a really long one but I think the most important part is this):
main.cpp:9:30: note: mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'const char [2]'
While compiling this bit of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = getline(cin, " ");
return 0;
}
The lines in the error won't match with the ones in the code I brought up here because I don't know how to make a new line whilst writing code in the Stack Overflow editor; I'm new here ;) Anyways, the error points to the line with the declaration of string x.
Basically what I want this code to do is to get a line from the user until he/she hits space. Maybe I'm doing something wrong from the beginning, so I'm open for suggestions of fixing this problem. (I'm not very experienced in C++, it's just that my teacher required to complete a task using this language.) Thanks,
Anthony
The second parameter of std::getline() is a reference to a std::string variable that accepts the read data. The string is not outputted in the function's return value.
Also, std::getline() does not accept a string for the delimiter. It takes only a single character.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string x;
getline(cin, x, ' ');
return 0;
}
I have the task to write a function that reads a line from a text file (renamed to .dat, however it contains only text), but I am out of options for a solution because of the following points:
I am using Borland C++ Version 5.02, and no, I CAN´T download another compiler because I dont have admin rights on my laptop and the guy who has the needed password isnt there until next week.
The compiler does not accept using namespace std, and also it doesnt accept getline(), no matter if string and iostream are included or not.
I am trying to find a solution or at least the tiniest approach, but I am unable to find one.
So my question is: How do I read a line from a simple textfile without using getline() (cin.getline works, the ones from string or fstream doesnt) ? The textfile contains several lines like these:
1234;12.05.03;08:44:23; XY12-AB;A1-12;Timeout
2345;12.05.03;09:04:34;XY1-CD;A22-9;Connection refused
And the numbers/letters between the ; need to be stored in variables so they can be worked with.
Im not asking for you to write my code, but I am reallyreaylly frustrated and my instructor is no help.
Live long and prosper,
Me.
http://www.cplusplus.com/reference/string/string/getline/
If you cant use (which you really shouldnt)
using namespace std
Then refer to your namespace with :: operator
For example:
std::string
Now try to write your own code using std:: and comment if you still cant do it.
Also, there is a lot of other options than std::getline() to read line of text.
Ref: Read file line by line
Option 1:
Try using the C's fgets() function.
Option 2:
You mention that cin.getline() works. You can freopen stdin with the input file and then cin will point to mentioned file. After that cin.getline() will read from the file:
Downside: After the freopen you will not be able to accept any input from the user.
Example: Note this has not been tried using g++ but I guess it should work with Borland too.
#include <iostream>
#include <stdio.h>
int main() {
char buf[1000];
freopen("somefile.dat", "r", stdin);
while (cin.getline(buf, sizeof(buf)).good()) {
// Now buf contains a line
// Do something with it
}
return 0;
}
Try using
getline(cin >> ws, variableName);
But first, you have to use
using namespace std;
I'm having the same problem while i using multi dimensional array on structs into a file, i have try different ways. But then i tried this one and it's work for me.
So in my case it gonna be like this
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myFile;
myFile.open("test.txt");
int count = 0;
while (!myFile.eof())
{
getline(myFile >> ws, data[count].string1);
getline(myFile >> ws, data[count].string2);
myFile >> data[count].int1;
for (int i = 0; i < data[count].int1; i++) {
getline(myFile >> ws, data[count].data2[i].string3);
}
count++;
}
return 0;
}
For more : https://www.geeksforgeeks.org/problem-with-getline-after-cin/
A .csv file is written like this:
M9005U00-X30A0S00-1;BAS;X;-0.002;-0.095
S707RY00-X30AOS00-1;HMV;X;+0.002;+0.081
W3005U00-X30BOJ00-1;BAS;X;+0.026;-0.138
H307QZ00-X30BOJ00-1;HMV;X;-0.025;+0.122
....
now I want to create a function, i.e.
double find_and_extract (string sss)
when this function is used with a keyword as its parameter, for example
find_and_extract (W3005U00-X30BOJ00-1);
it will search in the .csv file line by line, find corresponding line (in this case it should be the third line), and extract the certin part "+0.026" in this line, return as a double.
How should I write this function?
edit: Here is the code i've written so far:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <stdlib.h>
using namespace std;
void main()
{
find_and_extract (W3005U00-X30BOJ00-1);
}
double find_and_extract (string sss)
{
vector<string> vecarray;
ifstream infile("C:\\Data\\testdata.csv");
string temppo;
string contnt;
char csv_extract[40];
stringstream ss;
vector <string>::iterator ptr;
while (!infile.eof())
{
infile.getline(csv_extract,40);
ss << csv_extract;
ss >> contnt;
vecarray.push_back(contnt);
}
for (ptr=vecarray.begin();ptr!=vecarray.end();ptr++)
{
if ((*ptr).find(sss)==0)
temppo = (*ptr).substr(27,6);
}
return (strtod(temppo.c_str(),NULL,0));
}
Could anyone help me out to point out the errors?
Seeing as you already have the file as a string, I'd use the Knuth–Morris–Pratt algorithm to find the key, find the position of the 3rd and 4th semi-colons on that line and return the string in between them.
That's just an outline - you'll need to add error handling.
Check out strtok(). This is actually a pretty trivial task, and should be a good learning project if you are still new to C++.
You could use sed: This way, you could search for the key very efficiently, without having to implement an algorithm yourself. When you found the key, you can let sed output the parts of the line you need (use regular expressions to describe the pattern and groupings to print only part of it). After that, it's a simple string to float conversion that can be done in a programming language of your choice.
For starters:
sed -n 's/RegexToMatchYourKeyAndValues/MatchedValues/p'
If the text lines in the file are the same length, you may want to read the lines as blocks (i.e. many lines == 1 block) into a buffer, then search the buffer.
Your performance bottleneck will the reading the data from the file. In general, the search method you choose will be faster than reading in the data.