Palindrome

Hi, I need some help with my Hw. I just need some hints for part e. This is my assignment. http://www.cs.hunter.cuny.edu/~saad/courses/c++/hw/hw7.pdf
I managed to get up to part e and may need help for f. I have no idea how exactly I'm supposed to use my other functions. My main right now is just to test out what values come out.
What I was thinking of for that intit function was to do a loop beginning at neq and ending at peq. Then I had to divide by half to get the middle values... then see if it's a ' ', but then I get lost when it comes to getting l when the palindrome is "no x in nixon". Sorry if I'm not very clear.
I just need to figure out how to start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstring>
using namespace std;

int next(string s,int i,int j){
for(int k = 0; k<j; k++){
    if (s[k] != ' '){
        return k+1;}
    }
    return -1;
}

int prev(string s, int i, int j){
for (int k = j; k>i; k--)
    if (s[k] != ' '){
        return k-1;
    }
    return -1;
}

int neq(string s,int i,int j){
for(int k=0;k<j;k++)
if(s[k]!=' '){
    return k;
}return -1;
}
int peq(string s,int i,int j){
for(int k=j;k>=i;k--)
if(s[k]!=' '){
    return k;}
    return -1;
}
void intit(const char * s,int i,int j,int * k, int * l){
for(i=next(s,0,strlen(s));i<prev(s,0,strlen(s));i++){

}

}
int main(){
const char *a="evil olive";
cout << next(a,0,strlen(a))<<endl; // smallest k between
cout << prev(a,0,strlen(a))<<endl; // largest k between
cout << neq(a,0,strlen(a))<<endl;  // smallest k between including i end point
cout << peq(a,0,strlen(a))<<endl; // largest k between including j end point


return 0;
}

Thanks for reading.
Last edited on
How about this?
-- Start with lower at i (or neq(s, i, j) -- doesn't really matter, I suppose) and upper at j (or peq(s, i, j), if you'd like).
-- In each iteration of the loop, set lower equal to the next non-space character between lower and j, and set upper equal to the last (previous) non-space character between i and upper. In other words, you have lower and upper honing in on each other towards the middle.
-- Obviously, the loop should stop when lower and upper meet up with each other. Note that lower and upper won't necessarily be equal when this happens.

That's the basic gist of one way to approach the problem.
I'll let you figure out the specifics yourself....
Aha thank you very much :)
I had bit of a problem with the pointers in "init" but I got it eventually.
Topic archived. No new replies allowed.