user3213110
user3213110

Reputation: 199

How to return a const char* in a function?

I'm giving you guys my whole code. It's a bit long so I most sincerely hope you get it:

#include <iostream>
using namespace std;

char const* reverseWordsOnly(const char* s)
{
    int k;
    int p = strlen(s);


    for (int i = 0; i <= p; i++)
    {
        if (s[i] == ' ')
        {

            for (k = i - 1; (k != -1) && (s[k] != ' ') && (s[k] != ',') && (s[k] != ';') && (s[k] != '.'); k--)
                return &s[k];
            return " ";

        }
        if (s[i] == ',')
        {

            for (k = i - 1; (k != -1) && (s[k] != ' ') && (s[k] != ',') && (s[k] != ';') && (s[k] != '.'); k--)
                return &s[k];
            return ",";

        }
        if (s[i] == ';')
        {

            for (k = i - 1; (k != -1) && (s[k] != ' ') && (s[k] != ',') && (s[k] != ';') && (s[k] != '.'); k--)
                return &s[k];
            return ";";

        }
        if (s[i] == '.')
        {

            for (k = i - 1; (k != -1) && (s[k] != ' ') && (s[k] != ',') && (s[k] != ';') && (s[k] != '.'); k--)
                return &s[k];
            return ".";

        }
    }
}

int main()
{
    char s[1002];
    cin.getline(s, 1002, '\n');
    strcat(s, " ");
    reverseWordsOnly(s);
    return 0;
}

So what does it do.. It reverses the order of the words' letters in a string. For example, if you write down Something.. terribly wrong., it should return gnihtemoS.. ylbirret gnorw.The interval and the dot stand as delimiters. Problem is that I must use this very very precise function char const* reverseWordsOnly(const char*) and I have absolutely no idea how to make it work with those constants and pointers. The code I have written is fully compilable but returns nothing but a blank space in the console. What have I done wrong? Any ideas? How to return those pointer values in the function?

Upvotes: 0

Views: 6985

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

char const* reverseWordsOnly(const char* s){
    static string work;
    work = s;
    string::size_type start_pos = 0, end_pos;

    while(string::npos != (start_pos = work.find_first_not_of(" .,;", start_pos))){
        if(string::npos == (end_pos = work.find_first_of(" .,;", start_pos+1)))
            end_pos = work.length();
        reverse(&work[start_pos], &work[end_pos]);
        start_pos = end_pos + 1;
    }

    return work.c_str();
}

int main(){
    cout << reverseWordsOnly("Something.. terribly wrong.") <<endl;

    return 0;
}

Upvotes: 5

Roy Spector
Roy Spector

Reputation: 151

Either I'm missing something, or this should help: 1. you get a const char *, even though you want to change it. the const keyword means your not suppose to change the content of the variable, so I would get rid of it. 2. It seems like your not actually changing the string, even though it's what your function should do (by the way you described it) 3. Try the strtok function to separate your sentence into words. 4. Don't return random strings in the middle of your function - write the words, reversed into a new buffer (allocated using the "new" keyword), and return the pointer to that buffer at the end of your function.

If this isn't enough let me know and I can reply with some code that should do the job.

Also... Your function returns char *, but in your main function you never assign its return value to a variable.

Upvotes: 1

Related Questions