For this assignment, you may not use any of the C-string library functions such
ID: 3597873 • Letter: F
Question
For this assignment, you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient.
This function returns true if the argument string is a palindrome. It returns false if it is not. A palindrome is a string that is spelled the same as its reverse. For example “abba” is a palindrome. So is “hannah”, “abc cba”, and “radar”.
bool isPalindrome(char *s)
Explanation / Answer
#include <iostream>
#include<cstring>
int getLength(char *s) {
int count = 0;
for(int i=0; s[i]!='';i++) {
count++;
}
return count;
}
bool isPalindrome(char *s) {
int n = getLength(s);
for(int i=0; s[i]!='';i++) {
if(s[i] != s[n-1-i]) {
return false;
}
}
return true;
}
using namespace std;
int main(){
char s[50]="abc cba";
cout<<isPalindrome(s)<<endl;
char s1[50]="hannah";
cout<<isPalindrome(s1)<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.