I've come into some problem with my code. At a first glance, it seems that I've done everything right and the code should work. But, it isn't. This exercise asks me to verify if a word is equal to it's reverse.
This is my code:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char cuv[255], cuv1[255]=""; cin>>cuv;
int j = strlen(cuv)-1;
for(int i = 0; i<strlen(cuv); i++)
cuv1[i]=cuv[j-i];
cuv1[strlen(cuv1)+1]='\0';
if(cuv==cuv1)
cout<<"cuvantul este palindrom";
else
cout<<"cuvantul nu este palindrom";
return 0;
}
This becomes much easier with std::string and std::equal:
std::string cuv = "ANNA";
return std::equal(std::begin(cuv), std::end(cuv), std::rbegin(cuv));
Or if for some reason you want to stick with C strings:
char cuv[255] = "ANNA";
int len = strlen(cuv);
return std::equal(cuv, cuv+len, std::reverse_iterator<char*>(cuv+len));
This method works fine, I've gave up trying to compare the words, so I compared the word's letters from start with those from the end.
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char cuv[255]; cin>>cuv;
int n = strlen(cuv);
int k = 0;
for(int i = 0; i!=strlen(cuv)/2;i++)
if(cuv[i]!=cuv[n-i-1])
k=1;
if(k==0)
cout<<"cuv pal";
else
cout<<"cuv != pal";
return 0;
}
problem is with this statement
cuv1[strlen(cuv1)+1]='\0'
Change this to
cuv1[strlen(cuv1)]='\0';
Related
I'm getting this error hence I am new to c++ I could not understand
please help me!!!
I am writing a palindrome code
This is the code given below:.............
I am basically here using some extra concepts not doing in-direct fashion.
if anyone can post the correct code he/she is most welcome...
//palindrome
#include <cstring> //or use #include <string.h>
#include <iostream>
using namespace std;
void Palindrom(string& );
void Palindrome(string& word)// same as (const string& word)
{
int n = word.length();
string word2;
char reverse[n];
for(int i = 0; i <=n; i++){
word[i]=tolower(word[i]);
}
word2=word; //now both are small
for(int i = n-1; i >=0; i--){
reverse[n-1-i]=word2[i];
cout<<reverse[n-1-i];
}
for(int i =0; i >n; i++){ //printing reversed
cout<< " Reverse: "<<reverse[i]<<endl;
}
// word is ok and word2 gets reversed
for (int i = 0; i <= n; i++){
if(word[i]==reverse[i])
{
cout<<"\nit is palandrome ";
}
cout<<"\nit is not a palindrome ";
}
}
int main()
{ string k="YuUuy";
void Palindrome(k);
return 0;
}
Correct syntax for calling a function is Palindrome(k); without the void.
Few remarks:
Get a good c++
book.
// same as (const string& word) is not true.
You did not include <string> header.
It's good practice to use std::size_t for indices, but beware of unsigned>=0 condition being always true.
char reverse[n]; is wrong, n must be a compile-time constant, VLA are not part of the C++ standard.
Function calls should not have return type. Change void Palindrome(k); in main() function to Palindrome(k);
While declaring array, the expression should have constant value. So you can't use char reverse[n];. Change it to char *reverse = new char[n]; and deallocate it using delete[] reverse; after you are done using it.
I would recommend you to use smart pointer. You should also take a look at std::string instead of using stream of char.
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'm using C++. So far, my code goes like this:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
int main() {
char word[100]; int ctr, count = 0;
printf("Enter string: "); gets(word);
ctr = 1;
while (word[ctr] != '\0') {
if (word[ctr-1] == word[ctr]) count++;
ctr++;
}
printf("%d", count);
return 0;
}
Sample Run
Enter string: mississippi
3
Enter string: mmmmrrnzzz
6
I've got the first sample run correctly (mississippi) with only 3 characters appearing twice consecutively but not on the second sample run (mmmmrrnzzz) with output 6.
My problem is that, it should not be 6 but 4 instead. 1 for the first two consecutive m, another separate 1 for the next two consecutive m, 1 for r, and 1 for z. I want a separate count for the first "mm" and the second "mm" and also for the "zz" but I don't know how.
I'm a freshman and very new to programming. I wish I could explain better. I'm hoping you could help me. Thank you.
In case of multiple couples like mmmm you need to make a double incrementation of your counter:
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];
int ctr;
int count = 0;
printf("Enter string: ");
gets(word);
int len = strlen(word);
ctr = 1;
while (ctr<len) {
if (word[ctr-1] == word[ctr])
{
count++;
ctr++;
}
ctr++;
}
printf("%d", count);
return 0;
}
First of all the program looks like a C program. In fact you are not using C++. You are using C.:) At least for example in C++ you should use header
#include <cstdio>
instead of
#include <stdio.h>
and so on.
And moreover it has a bug because in general the string can be empty. In this case the condition of the loop skips the first zero-terminating character and the program has undefined behaviour.
Here is a correct approach
#include <stdio.h>
int main( void )
{
const char *s = "mmmmrrnzzz";
size_t count = 0;
while ( *s++ )
{
if ( *s == *( s - 1) )
{
++count;
++s;
}
}
printf( "count = %zu\n", count );
}
The output is
count = 4
Take into account that function gets is unsafe and is not supported by the C (or C++) Standard any more.
You should use function fgets instead of gets.
This will work
#include <stdio.h>
#include <string.h>
int main() {
char word[100]; int ctr, count = 0;
printf("Enter string: "); gets(word);
int len=strlen(word);
ctr = 1;
while (ctr<len) {
if (word[ctr-1] == word[ctr])
{
count++;
ctr++;
}
ctr++;
}
printf("%d", count);
return 0;
}
A standard library version:
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
int count{};
std::string s;
std::cin >> s;
for (auto it = s.begin(); (it = std::adjacent_find(it, s.end())) != s.end(); it += 2)
++count;
std::cout << count << '\n';
}
So I'm teaching myself C++ and I'm struggling to understand why this code keeps crashing. I've identified that this line: string str = to_string(n) is probably incorrect. But I'm not seeing the other errors for why it's crashing.
#include <iostream>
#include <string>
using namespace std;
void write_vertically(int n)
{
string str = to_string(n);
if (str.length()>=0)
{
cout<<stoi(str.substr(0,1))<<endl;
write_vertically(stoi(str.substr(1,str.length())));
}
}
int main( )
{
write_vertically(1234567890);
return 0;
}
You are having a Stack Overflow! And you're on the perfect website to find a solution to that.
In the line string str = to_string(n);
No matter the value of n, to_string is going to return a non-empty string, which could be "0", "6" or "1653", whatever.
The end condition for your recursion is if (str.length() >= 0) is false.
However, as stated above that is never false.
What did you intend the end condition of your recursion to be? Maybe we can help you with that.
Edit: It turns out that the code should crash before going into a stack overflow, because it would end up calling stoi with an empty string, which makes it throw an std::invalid_argument. However, there was still an infinite recursion problem, so I will keep my answer up.
You are calling stoi("") at the end
#include <iostream>
#include <string>
using namespace std;
void write_vertically(int n){
string str = to_string(n);
cout<<stoi(str.substr(0,1))<<endl;
if (str.length()>1)
write_vertically(stoi(str.substr(1,str.length())));
}
int main( ) {
write_vertically(1234567890);
return 0;
}
https://ideone.com/YfYhZw
You are doing a lot of (unnecessary) type conversion. Here's a way to accomplish your goal without using strings.
#include <iostream>
using namespace std;
void write_vertically( unsigned int n ) {
unsigned int d = n % 10;
n /= 10;
if( n )
write_vertically( n );
cout << d << endl;
}
int main() {
write_vertically(1234567890);
return 0;
}
You have to change your recursion condition as follows:
if (str.length()> 0) {
cout<<stoi(str.substr(0,1))<<endl;
if(str.length() > 1)
write_vertically(stoi(str.substr(1,str.length())));
}
}
Demo: http://coliru.stacked-crooked.com/a/ecd26e57c45cea2b
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);
}