The function is to output the phrase one or more in single line.

Please I need help i have this final to turn in tomorrow.i recall in the past i wrote a similar program like this all so i need somebody to lead me into changin this program so that it can fit the requirement of my assignment.


My requirement is assignmet is: I need to write a function which is to input a phrase one or more words on a single line ) and then display the phrase in reverse order -note that you must handle the inputof spaces.

It then to search the original phrase to see if the word "junk" is in and output a message stating if it is or not and if it is, its location in the phrase .

Attached is the program is wrote few day ago. How can i change this to fit my the requirement of the program.

#include <stdio.h>

void rev(char *l, char *r);


int main(int argc, char *argv[])
{
char buf[] = "the world will go on forever";
char *end, *x, *y;

// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);


// Now swap each word within sentence...
x = buf-1;
y = buf;

while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}
// Now print the final string....
printf("%s\n",buf);

return(0);

}


// Function to reverse a string in place...
void rev(char *l,char *r)
{
char t;
while(l<r)
{
t = *l;
*l++ = *r;
*r-- = t;
}

}
Last edited on
closed account (3qX21hU5)
First please use codetags when posting code in the forums (Hint: the <> off to your right when replying) it makes it so that the code is much easier to read.

Well what is your question exactly? I understand your assignment but I'm not sure where you are stuck at. If you are having problems getting started I would recommend these things.

1) You will probably want to use strings to hold your phrase, unless your professor stated that you need to use char arrays.

2) Use the getline() function to record the whole phrase into one string

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string phrase;

    getline(cin, phrase);

    cout << phrase << endl;
}


3) Search for the word "junk". You can use strstr like KRAkatau mentioned for this.

Now personally I would say to start from scratch since these programs are pretty short and it would probably take longer to not start a new project.

If you get stuck anywhere just post your problem and we will try helping you out.
Last edited on
Topic archived. No new replies allowed.