I have this code in C language and what I'm looking for is to save the phrase entered by keyboard in another string but I don't understand why it doesn't work the other way around. #include
#include <string.h>
#include <ctype.h>
#define MAX 1000
int main (void)
{
int i,j,k;//Indices.
char frase[MAX],invertida[MAX],auxiliar[MAX];//Cadenas.
printf("Programa en el que introduces una frase para rescribirla al reves\n\n");
printf("Introduce tu frase: ");
gets(frase);
strcpy(auxiliar,frase);
j=strlen(frase);
i=0;k=0;
while(auxiliar[k]!='\0' && j>=0)
{
invertida[i]=frase[j];
invertida[i+1]='\0';
i++;k++;j--;
}
printf("La frase invertida es:\n%s",invertida);
return 0;
}
I know how to print the original string backwards but I don't understand why it doesn't work when I try to save it to another string. Please help.
the first character of "inverted" will always be null.
If phrase is "hello", j will be 4. But remember that in C arrays start from zero, so
j[0] = 'h'
,j[1] = 'o'
,j[2] ='l'
,j[3] = 'a'
and thereforej[4] = '\0'
.In the first line of the while,
invertida[i] = frase[j]
, soinvertida[0] = frase[4]
and there the first character remains null.make that
and it will work. I suggest you do the
invertida[i+1] = '\0';
outside of the loop, it will work the same.Problems in your code.
I see that your code uses the loop
while
for the string reversal process, but this is not usually the case. Although it is true that with a loop itwhile
works, the general rule is to use this type of loop for repetitive tasks whose number of iterations is not known, while the loopfor
is used for those repetitive tasks whose number of iterations is known. In your case, following the general rule you should use a loopfor
.You use three 1 formations for a task that can be done with two (original chain and reverse chain). For some reason you think you need to copy the original data into a helper 1 array to invert it, which spends more processing time (calling
strcopy
spends O(n) ) as well as spending more memory (you spend 1,000 more bytes with the helper array).You don't initialize your variables, so they contain random data and so you need to take an extra step to add the string terminator in
invertida
, which you are also unnecessarily assigning to each round of the loop by overworking the processor.You shouldn't use single letter variables, in the 80's and 90's computer screens supported 80 characters width and it made sense to save screen space; nowadays it is absurd and will only make your code harder for you and your peers to understand.
Finally, you're calling
strlen
(also O(n) ) to find out the length of the string when you could find out the length at read time.Proposal.
Using
%n
you can find out the size of the read data, so:But this would cause it to read into space, so it wouldn't work for sentences. To avoid that you can use the catch
[colección de caracteres]
which works the same as the regular expression catch :Taking all of the above into account, your code could look like this:
You can see the code working in Wandbox 三へ( へ՞ਊ ՞)へ ハッハッ.
1 Also known as array, or in English: array.
Note, inv is the inverted phrase and I create it outside of main because inside the empty spaces of the array would be filled randomly and that would cause it to show not only the phrase but also a lot of special characters, in the while I iterate in such a way that, for example for the word 'hello':
in this way we go through the two sentences, the reverse from front to back and the sentence from back to front