C++.. program that allows the user to enter 4 characters.rogram must check if the string is a palindrome?
-
A string is an array of characters. A palindrome is a string that is exactly the same when spelled backwards (e.g. abba, hannah are both palindromes). Write a program that defines a char array with 4 elements and allows the user to enter 4 characters. Your program must check if the string is a palindrome. (Hint: you will need to use another char array with 4 elements). Only in my first year so need help with this and using inly basic stuff like arrays..Im completly lost with regards to this
-
Answer:
about checking palindromicity code: this the fourth or fifth time that there's such a request! try to seach y.answers about it! you'll find already made code. since the hint suggests you to use another 4-char long array, they are implicitly suggesting you how to code the function to check palindrome: reverse the string and compare the "straight" one with the reversed one. if they match, the word is palindrome. said that, since codes are already out there, i leave you to your mumbling
Stephen O at Yahoo! Answers Visit the source
Other answers
if you allow to use string than you can easily do it by using inbuilt function strcpy and strrev int main() { char a[5],b[5]; gets(a); strcpy(b,a); strrev(b); if(strcmp(b,a)==0) cout<<"String is palindrome"; }
Sounds like you want people to do your homework for you! Why don't you start learning about arrays and strings? Palindrome program is one of the simplest ones once you know the concepts. Let me give you a hint for your program. First read the entire string into an array. Use two "for loops", one from left to right of string and other right to left. And compare each array element for both loops to see if it is equal. Something like for(int i=0;i<leng;i++) for(j=leng-1;j>0;j--) a[i] = a[j]; a => string array leng => string length, calculated using the "strlen()" function. All a[i] and a[j] should be equal for a string with even number of characters. For odd number of characters, one center element will be left after all a[i]=a[j] I think this hint, and some study of arrays and strings should be sufficient to write the code yourself. If you get errors, post your code here for help.
See below for about the simplest way I can do it. Beware of the hint. Maybe you're being encouraged to use a second char array, but you don't need to. A good thing for you to try now is to make this work for a char array of any length. #include <iostream> #include <string> #include <sstream> using namespace std; const unsigned N = 4; int main(int argc, char *argv[]) { char a[N]; bool inputOk = false, isPalindrome = true; string line; stringstream ss; cout << "Enter " << N << " chars: "; do { getline(cin,line); ss.clear(); ss.str(line); inputOk = (ss >> a[0] >> a[1] >> a[2] >> a[3]); if (inputOk == false) { cout << "invalid input, try again ..." << endl << "> "; } } while (inputOk == false); for (unsigned i = 0, j = N-1; (i < j) && (isPalindrome == true); i++,j--) { isPalindrome = (a[i] == a[j]); } cout << "is "; if (isPalindrome == false) cout << "not "; cout << "palindrome." << endl; return 0; }
Related Q & A:
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Is there a program that allows me to make my mp3's files a ringtone?Best solution by play.google.com
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.