Question

What is happening to my palindrome program? C++. Here is what my code does. I have...

What is happening to my palindrome program? C++.

Here is what my code does. I have to strings. The user inputs into one of them. I use a for loop to enter the string into a second one in reverse. Then, I use strcmp to see if they are the same. If the output of strcmp is 0 the answer is trye, otherwise false. For some reason something is going really wrong with my copyString. I tried copying my for loop directly from a website to see if I was doing it wrong and i still get a weird value inside copString

#include<iostream>
#include<string.h>
#include<string>

using namespace std;

int main() {
   char string1[20], copyString[20];

   int i,x, length=0,j,flag=0;

   cout << "Enter a string: ";
   gets_s(string1);

   length = strlen(string1) - 1;

   for (i = length, j = 0; i >= 0; i--, j++) {
       copyString[j] = string1[i];
   }

   flag = (strcmp(string1, copyString));

   cout << flag << endl << "here is 1: " <<string1 << endl << endl <<copyString;

   if (flag == 0)
       cout << "True";
   else
       cout << "False";

}

Homework Answers

Answer #1

#include<iostream>
#include<string.h>
#include<string>

using namespace std;

int main() {
    char string1[20], copyString[20];

    int i, x, length = 0, j, flag = 0;

    cout << "Enter a string: ";
    gets_s(string1);

    length = strlen(string1) - 1;

    for (i = length, j = 0; i >= 0; i--, j++) {
        copyString[j] = string1[i];
    }
    copyString[j] = '\0';   // we have to end a string with '\0'. null-terminating character.

    flag = (strcmp(string1, copyString));

    cout << flag << endl << "here is 1: " << string1 << endl << endl << copyString;

    if (flag == 0)
        cout << "True";
    else
        cout << "False";
}


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put the result in st3 with a space separator. it has to be done using array representation of strings #include <iostream> using namespace std; #include <string> int main() { string st1,st2, st3; int c = 0, i =0;    cout << "Enter a string: "; cin >> st1; cout << "Enter another string: "; cin >> st2;    while (st1[c] != '\0'){ st3[c] = st1[c];...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
I'm writing a fairly simple program in C++ and i keep getting an error message when...
I'm writing a fairly simple program in C++ and i keep getting an error message when i try to compile it I don't see what I'm doing wrong if someone could look at this and point me in the right direction? #inlcude <iostream> using namespace std; int main() {    int length, width, area;       cout<<"This program calculates the area of a ";    cout<<"rectangle.\n";    cout<<"What is the length and width of the rectangle ";    cout<<"separated by...
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix?...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix? #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" int main() {       int n;    int a[10], i;    int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT