Jak zaimplementować wskazniki w tym kodzie

0

W poleceniu mam aby stworzyć rewers słów, np dla wejścia
Tom
Alfred

ma być output
moT
derflA.

Tylko, że jest taka notatka przy zadaniu: Do wczytania danych wejściowych wykorzystaj dynamicznie przydzielony fragment pamięci. Do iteracji po przetwarzanej tablicy znaków użyj wskaźnika.

Nie wiem co źle robię, bo program działa prawidłowo, a nie chce przejść przez system, który automatycznie sprawdza program.

void reverse(string tab) {
    string reversed = tab;
    std::reverse(reversed.begin(), reversed.end());
    cout << reversed << endl;
} 

int main()
{

int n;
cin >> n;

string *tab = new string[n];

string x;
for(int i=0; i<n; i++) {
  cin >> x;
  tab[i] = x;
}

for(int i=0; i<n; i++)
{ 
reverse(*tab);
tab++;
}

delete[] tab;

  return 0;
}
2

A nie chodzilo o char*?

"przetwarzanej tablicy znaków" bo tablica znakow to wlasnie char[] a nie string[]

I swoja droga to kto takie g***ne polecenie ulozyl jak mozna zrobic for (auto& str : strings)

1

Dobra widze juz problem.

Patrz:
delete[] tab; - ale Ty chwile wczesniej ten wskaznik rozwaliles robiac n razy tab++ i on jest nieprawidlowy:)

Na koncu sie pojawia blad munmap.

0
#include <iostream>
using namespace std;
#include <algorithm>

void reverse(string tab)
{
  string reversed = tab;
  std::reverse(reversed.begin(), reversed.end());
  cout << reversed << endl;
}

int main()
{

  int n;
  cin >> n;

  string *tab = new string[n];

  string x;
  for (int i = 0; i < n; i++)
  {
    cin >> x;
    tab[i] = x;
  }

  for (int i = 0; i < n; i++)
  {
    reverse(tab[i]);
    *(tab + i);
  }

  delete[] tab;

  return 0;
}
0

https://i.imgur.com/Bf6G7Fv.png

No to Twoj program nie spelnia specyfikacji, lol.

0

@stivens

mam tez takie coś, tylko, że tego też nie akceptuje, tu można dodać te wskaźniki?

#include <iostream>
#include <vector>
using namespace std;
#include <algorithm>

void reverse(string &tab)
{
  string reversed = tab;
  std::reverse(reversed.begin(), reversed.end());
  cout << reversed << endl;
}

int main()
{

  vector<string> tab;
  string x;

  while (cin >> x)
  {
    tab.push_back(x);
    for (int i = 0; i < tab.size(); i++)
    {
      reverse(tab[i]);
    }
  }

  return 0;
}
0

Tak w ogole to nie musisz pamietac tych wyrazow...

Wczytaj -> wypisz -> zapomnij

A nie dziala bo w kazdym obiegu petli wypisujesz to co juz znales wczesniej.

0

@stivens: doradzisz czy to idzie w dobrym kierunku?

#include <iostream>
using namespace std;
#include <cstring>

char* reverse(char* input) {
  int len = strlen(input);
  char temp[len];
  for(int i=len-1; i>len; --i) {
    temp[i]+=input[i];
  }
  return temp;
}
int main()
{

  char input[100];
  while(cin>>input) {
   cout << reverse(input);
  }

  return 0;
}
1

Zwracasz wskaznik na stos.


Jakbys uzywal flag kompilacji to bys wiedzial

stivens@S410 ~ $ ccpp test.cpp 
test.cpp: In function ‘char* reverse(char*)’:
test.cpp:11:10: warning: address of local variable ‘temp’ returned [-Wreturn-local-addr]
   11 |   return temp;
      |          ^~~~
test.cpp:7:8: note: declared here
    7 |   char temp[len];
      |        ^~~~


for(int i=len-1; i>len; --i) {

Co tu sie? Nie powinno byc jakies i >= 0? :)
No i warto pamietac ze cstring musi byc zakonczony znakiem '\0'

A wiec

char temp[len + 1];
temp[len] = '\0';
0

I w sumie dobrze kombinujesz ale ja bym do tego podszedl inaczej jesli chodzi o odwracanie

for(int i = 0; i < len; i++) {
    temp[i] = input[len-1 -i];
  }
0

tego tak nie robimy.

na charach to tak pójdzie:

char *reverse(char *s)
{
  for(int i = 0, j = strlen(s)-1; i < j; )
    s[i++] :=: s[j--]; // swab - wymiana...

 return s;
}

na string podobnie.

2
#include <iostream>

std::string reversed(const std::string& s)
{
    return {s.rbegin(), s.rend()};
}

int main()
{
    std::string s;
    while (std::cin >> s) {
         std::cout << reversed(s) << '\n';
    }
}

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