Tilt Posted May 2, 2013 Report Posted May 2, 2013 (edited) avem codul urmator #include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){int i;char s[50];cin.get (s,50);i=strlen(s);while (i) {cout << s;cout << endl;strcpy(s+strlen(s)-1, s+strlen(s)+1);i--;} system("PAUSE"); return EXIT_SUCCESS;}am observat ca, daca incerc sa introduc mai mult de 6 caractere cand compilez nu-mi mai afiseaza ce trebuie ( adica prefixele in ordine descrescatoare ) ... nu pot sa inteleg de ce; Edited May 2, 2013 by Tilt Quote
H3xoR Posted May 2, 2013 Report Posted May 2, 2013 (edited) #include <cstdlib>#include <iostream>#include <string.h>using namespace std;int main(int argc, char *argv[]){ int i; char s[50]; cin.get (s,50); i = strlen(s); while (i) { cout << s; cout << endl; [COLOR="#FF0000"]strcpy(s + i - 1, s + i);[/COLOR] i--; } system("PAUSE"); return EXIT_SUCCESS;}Nu prea ?tiu cum s?-?i explic pe în?elesul t?u. Încearc? s? deduci „ce ?i cum” din modificarea f?cut? de mine. Edited May 2, 2013 by H3xoR Quote
lutulik Posted May 2, 2013 Report Posted May 2, 2013 strcpy(s + i - 1, s + i);Sterge un caracter incepand cu pozitia i-1.superba i=7, sterge caracterul 6 => asuperb i=6, sterge caracterul 5 => bsuper i=5, sterge caracterul 4 => rsupe i=4, sterge caracterul 3 => esup i=3, sterge caracterul 2 => psu i=2, sterge caracterul 1 => us i=1, sterge caracterul 0 => si=0, se opreste.Tu aveai: strcpy(s+strlen(s)-1, s+strlen(s)+1);In care iti lua si caracterul NULL de la sfarsitul sirului, probabil de aia nu rula cum trebuie. Quote
Tilt Posted May 2, 2013 Author Report Posted May 2, 2013 (edited) deci teoretic eu iau NULL-ul si il deplasez peste cate un caracter la fiecare loop ? mai devreme luam ce era dupa NULL adica nimic ? si totusi cum de functiona programu la 6 caractere cand sirul era de 50 si la mai multe cand il faceam de 100 ... ? Edited May 2, 2013 by Tilt Quote
totti93 Posted May 2, 2013 Report Posted May 2, 2013 (edited) Eu m-as juca cu caracterul NULL:while (i) { cout << s; cout << endl; s[--i] = 0;}L.E.: Eventual schimba ordinea dintre afisare si atribuire sa nu afiseze inca odata cuvantul cu care ai inceput si sa afiseze si "":while (i) { s[--i] = 0; cout << s; cout << endl;} Edited May 2, 2013 by totti93 Quote
Nytro Posted May 2, 2013 Report Posted May 2, 2013 #include <string>using namespace std;string FOLOSITI_BA_STRING = "OBIECTE"; Quote