Program odwracający string z użyciem pointerów

0

Witam,
Bardzo proszę o pomoc. Łamię sobie głowę nad ćwiczeniem trzeci dzień, wydaje mi się że nic już nie wymyślę i zwracam się z prośbą o pomoc do Was.
Program ma przyjąć string od użytkownika i wydrukować string odwrotnie używając pointerów. Czy ja jestem w ogóle blisko?
Przy próbie uruchomienia programu wyskakują mi następujące błędy:

main.cpp: In function ‘int main()’:
main.cpp:38:29: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
   cout << reverseStr(pString) << endl;
                             ^
main.cpp:8:8: note:   initializing argument 1 of ‘char* reverseStr(char*)’
 char * reverseStr(char * pString) // this function reverses the string
        ^

Oto mój kod:

#include <string.h>
#include <iostream>
using namespace std;

char * reverseStr(char * pString) // this function reverses the string
{
	{
	    return NULL;
	}
	char * pStart = pString;
	char * pEnd = pStart + strlen(pString) -1;
	while (pEnd > pStart)
	{
	    char temp = *pStart;
	    *pStart = *pEnd;
	    *pEnd = temp;
	    pStart++;
	    pEnd--;
	}
	return pString;
	
}

int main()
{
    char pString;
	char run_me_again_check; // for mutiple running of program
	bool run_me_again = true;

	while (run_me_again)
	{
	    cout << "Please enter the string to reverse : ";
		cin >> pString;
		reverseStr;
		cout << reverseStr(pString) << endl;
		cout << "One more time? (Y/N)  ";
		cin >> run_me_again_check;

		if (run_me_again_check == 'Y' || run_me_again_check == 'y')
		{
			run_me_again = true;
		}

		else
		{
			run_me_again = false;
		}

	}
	return 0;

}
1

Przyjmujesz char od użytkownika (char pString; cin >> pString;), a Twoja funkcja oczekuje _char * _ (char * reverseStr(char * pString)).

Skorzystaj po prostu, jeśli możesz, ze stringów z C++, zamiast kombinować coś na charach.

2

To na pewno zajęcia z C++? Bo w C++ takie coś robisz za pomocą reverse iteratorów, zamiast bawić się w arytmetykę wskaźników.

0

Tak, to C++; niestety muszę użyć wskaźników... Czy ktoś mógłby mi pokazać jak powinno to wyglądać? Mam jeszcze kilka podobnych ćwiczeń i myślę że dopóki nie ogarnę tego "prostego" ćwiczenia to całość nadal będzie dla mnie abstrakcyjna. Bardzo chcę zrozumieć ale coś mi umyka.

1

jak masz po prostu wypisać, to przekaż wskaźniki na początek i koniec stringa i wypisuj od tyłu (czyli dekrementuj wskaźnik):

void print_reverse(char const* begin, char const* end)
{
    while(end-- > begin) {
        putchar(*end);
    }
}

https://wandbox.org/permlink/NXElzZDOxBOxVzmy

0

Strasznie mi głupio, ale naprawdę bardzo chcę to zrozumieć. Czy możecie spojrzeń na kod?
Dostaję wiadomość :

main.cpp: In function ‘void print_reverse(const char*, const char*)’:
main.cpp:11:20: error: too few arguments to function ‘int putc(int, FILE*)’
         putc(*P_END);
                    ^
In file included from /usr/include/c++/5/cstdio:42:0,
                 from /usr/include/c++/5/ext/string_conversions.h:43,
                 from /usr/include/c++/5/bits/basic_string.h:5334,
                 from /usr/include/c++/5/string:52,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:2:
/usr/include/stdio.h:574:12: note: declared here
 extern int putc (int __c, FILE *__stream);
            ^
main.cpp: In function ‘int main()’:
main.cpp:24:38: error: too few arguments to function ‘void print_reverse(const char*, const char*)’
         cout << print_reverse(pString) << endl;
                                      ^
main.cpp:7:6: note: declared here
 void print_reverse(char const * P_START, char const * P_END)
      ^
#include <string.h>
#include <iostream>
using namespace std;
char * pString;
void print_reverse(char const * P_START, char const * P_END)
{
    while(P_END --> P_START) 
    {
        putc(*P_END);
    }
}

int main()
{
    char run_me_again_check; // for mutiple running of program
    bool run_me_again = true;
 
    while (run_me_again)
    {
        cout << "Please enter the string to reverse : ";
        cin >> pString;
        cout << print_reverse(pString) << endl;
        cout << "One more time? (Y/N)  ";
        cin >> run_me_again_check;
 
        if (run_me_again_check == 'Y' || run_me_again_check == 'y')
        {
            run_me_again = true;
        }
 
        else
        {
            run_me_again = false;
        }
 
    }
    return 0;
 
}
1

Przepraszam, zapomniałem poprawić w listingu putc na putchar

Swoją drogą:

  • pString masz globalny i nigdzie nie alokujesz na niego pamięci. Użyj zwykłej lokalnej tablicy znaków lub std::string
  • print_reverse przyjmuje 2 parametry, a przekazujesz jeden

1 użytkowników online, w tym zalogowanych: 0, gości: 1