¿Esta función de cadena inversa en C está mal escrita? / ¿Cómo mejorar este código? [cerrado]
Frecuentes
Visto 109 equipos
-2
Here is a part of code which describes a function to reverse characters of a string
(Based on exercise 1-19 of Brian W. Kernighnan Programming in C)...
( I have googled various text reverse function, but all of them us pointers or using strrev(), but I don't have an idea what a pointer is.... nor do I want to use strrev(), hence I made a reverse string function as the author wanted it to be........ )
La función:
void reverse(char s[])
{
int i , n ;
char j ;
i = 0 ;
while(s[i] != '0') //And not EOF
{
++n;
}
for(i = 0; i < n; ++i)
{
j = s[i] ;
s[i] = s[n - 1] ;
s[n - 1] = j ;
}
}
However I think overwritting arrays is bad , and the whole function seems awry.
P.S : It would be great if you did check and help me with the whole code here, since it would be offtopic if I did post it here, The code's main return 0; however it still doesn't work....
[EDIT] Ok I am seriously sorry for troubling you for a typo... I can't delete this question since it has answers with upvotes however I'm sorry....
The correct function would be :
void reverse(char s[])
{
int i, l;
char temp;
for (l = 0; s[l] != '\0'; ++l);
l--;
for (i = 0; i < l; ++i) {
temp = s[i];
s[i] = s[l-1];
s[l-1] = temp;
--l;
}
}
Full Code is here :
Code Working is here :
ACTUALIZACIÓN:
I created a correct and working solution for the word 'hello':
#include <stdio.h>
int main(void)
{
char s[] = "hello";
char temp;
// do the swapping here..
temp = s[0];
s[0] = s[4] ;
s[4] = temp ;
temp = s[1] ;
s[1] = s[3] ;
s[3] = temp ;
temp = s[2] ;
s[2] = s[2] ;
s[2] = temp ;
printf("%c, %s ", temp, s);
}
2 Respuestas
4
I don't know where you got this piece of code, but it's actually quite broken:
n
is never initialized. This is undefined behavior.The while loop won't terminate at all because it compares a
char
with a value that is not in the range of achar
.The while loop can't do anything sensible since its body can't change the loop condition.
The for loop exchanges all characters with one single array element, which effectively rotates the string right by one char. But the program will never reach this point anyway.
Respondido el 12 de junio de 14 a las 11:06
3
El lazo
while(s[i] != EOF)
{
++n;
}
parece incorrecto
- String end is checked via
0
, no a través deEOF
. - Nunca cambias
i
, so that you always checks[0]
.
Respondido el 12 de junio de 14 a las 11:06
So shall I make it to s[i] = '0'; ?? - ArchKudo
@ArchKudo No, you should make it s[i] != 0
. - cmaster - reinstalar a monica
@ArchKudo And, as said, additionally do n = 0;
y use s[n] != 0
. - glglgl
@ArchKudo You are getting much closer. There is still an issue with it, but why don't you try to compile your code and see if it does what you want? That way you learn to debug your code yourself, which is essential to learning programming. - cmaster - reinstalar a monica
@glglgl Actually, the loop for (n = 0; s[n] != '\0'; ++n) ;
is fine, it correctly increments n
until it points to the terminating null byte. However, the following statement isn't. - cmaster - reinstalar a monica
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c function or haz tu propia pregunta.
Questions asking to improve and critique functioning code are more suitable at Revisión de código. - JJJ
Strings in C are usually nul terminated (
0
or'\0'
), noEOF
which is a negative integer. - user694733@Juhana but how do I migrate to Code Review since I have a rep of just 47 - ArchKudo
@Juhana this code is very much not functioning. - n. m.
your while is an endless loop and what you are doing with n is undefined behaviour - mch