Jump to content
Tilt

Dev C++ [siruri neclaritate]

Recommended Posts

Posted (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;

46jZE7r.png

Edited by Tilt
Posted (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 by H3xoR
Posted
strcpy(s + i - 1, s + i);

Sterge un caracter incepand cu pozitia i-1.

superba i=7, sterge caracterul 6 => a
superb i=6, sterge caracterul 5 => b
super i=5, sterge caracterul 4 => r
supe i=4, sterge caracterul 3 => e
sup i=3, sterge caracterul 2 => p
su i=2, sterge caracterul 1 => u
s i=1, sterge caracterul 0 => s
i=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.

Posted (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 by Tilt
Posted (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 by totti93

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...