C++ please help.. Asking Again..Right Heree.. Palindromic Numbers?
-
I already write a Palindromic Number codes and I showed my teacher. The teacher said you need to rewrite your codes again and wants to puts these expressions.. The program must be split into the following functions. So, the following codes are my original codes.. #include <iostream> #include<cstdlib> #include<cctype> #include<cstring> using namespace std; void Reverse(char name[]); bool ispalindrome(char x[]); bool isrepdigit(char x[]); int main(int, char**) { int EXIT_VALUE = 0; char strn[80]; char arr[80]; bool flag=false; do{ cout<<"Enter a positive integer ("<<EXIT_VALUE<<") to exit: "; cin>>strn; if(atoi(strn)<0){ cout<<"*** error: inputs must be greater than zero"<<endl<<endl; continue; }else if (!isdigit(strn[0])) { cout << "*** error: inputs must be an integer" << endl<<endl; continue; } strcpy(arr,strn); Reverse(arr); if(strcmp(arr,strn)==0) { flag=true; } if(flag){ cout<<strn<<" is a Palindrome"<<endl<<endl; }else{ cout<<strn<<" is not a Palindrome"<<endl<<endl; } flag=false; }while(strcmp("0",strn)!=0); return 0; } void Reverse(char name[]) { // get the length of the current word in the array index int nameLength = strlen(name)-1; for (int currentChar=0; currentChar < nameLength; --nameLength, ++currentChar) { // copy 1st letter in the array index into temp char temp = name[currentChar]; // copy last letter in the array index into the 1st array index name[currentChar] = name[nameLength]; // copy temp into last array index name[nameLength] = temp; } } bool ispalindrome(char[]); Palindromic number equals itself when its digits are reversed. Examples: 101, 2112, 12321. bool isrepdigit(char[]); The digits of a repdigit number are all the same. Repdigit numbers must have at least two digits. Examples: 22, 111, 444, 999999. bool isundulating(char []); An undulating number has the digit form aba, abab, ababa..., where a != b. Examples: 101, 121, 6969, 12121. void scramble(char[] orig, char[] new); convert odd digits into nothing convert 0 into a convert 2 into e convert 4 into i convert 6 into o convert 8 into u
-
Answer:
If you're using c++, you're ,making your life complicated for no reason. #include <iostream> #include <string> #include <cstdlib> #include <cctype> using namespace std; main() { string num; // store the number as a string to begin with cout<<"Enter a positive integer:"; cin>>num; if (!isAVaildNumber(num)) { cerr<<"Number is no good"<<endl; exit(1); } cout<<boolalpha<<"Is "<<num<<" a palindrome? "<<isPalindrome(num)<<endl; //call all other functions similarly } bool isAValidNumber(string& num) { for (int i=0; i<num.length( ) ; i++) { if (!isdigit(num[0])) { return false; } } return true; }
Lebron at Yahoo! Answers Visit the source
Other answers
There are many ways to do this, and you should be able to work out a solution on your own. There's no reason to use C-strings, use C++ string objects instead. There are also many other good C++ features you can use to your advantage, as I've done below. If you say, "but I don't know that <function, class, etc.>", well, look it up and learn, that's why you're taking a C++ course. #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <set> #include <algorithm> #include <cctype> using namespace std; bool isPal(const string &s) { string sRev(s); reverse(sRev.begin(), sRev.end()); return s == sRev; } bool isRep(const string &s) { return (s.size() > 1) && (set<char>(s.begin(), s.end()).size() == 1); } bool isUnd(const string &s) { bool und = ((s.size() > 2) && (s[0] != s[1]) && (s[0] == s[2])); for (size_t i = 3; (und == true) && (i < s.size()); i++) { und = (s[i] == s[i - 2]); } return und; } void scramble(const string &s0, string &s1) { static const char t[] = { 'a', ' ', 'e', ' ', 'i', ' ', 'o', ' ', 'u' }; s1.clear(); for (size_t i = 0; i < s0.size(); i++) { if (isdigit(s0[i]) && (!(s0[i] & 1))) { s1.append(1, t[s0[i] - '0']); } } } int main(int argc, char *argv[]) { unsigned n; stringstream ss; string in, scrambled; cout << "Enter unsigned integers:" << endl; while (true) { cout << "> "; getline(cin, in); ss.clear(); ss.str(in); if ((ss >> n) && (!ss.good())) { cout << "isPal : " << boolalpha << isPal(ss.str()) << endl; cout << "isRep : " << isRep(ss.str()) << endl; cout << "isUnd : " << isUnd(ss.str()) << endl; scramble(ss.str(), scrambled); cout << "scramble: \"" << scrambled << "\"" << endl; } } return 0; }
Related Q & A:
- Why cant i view my contacts display image? please help if you know ,thanks?Best solution by Yahoo! Answers
- The pores on my face are wideninf by the minute. Please help me so that I can have them the normal size?Best solution by Yahoo! Answers
- I cannot get the toolbar with my favorites to appear...please help.Best solution by Yahoo! Answers
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- Help with TV show, please help?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.