[英]Reversing a string in c, problem using strlen function
I wrote a code to reverse a string, but the strlen
function is giving me the wrong length of string, that's why the reversing of the string is not done properly.我写了一个代码来反转一个字符串,但是
strlen
函数给了我错误的字符串长度,这就是为什么没有正确完成字符串的反转。 Here is the code I wrote:这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reversestring(char string[], int start, int end);
int main() {
char str[500];
int n;
n = strlen(str);
reversestring(str, 0, n - 1);
printf("%d\n", n);
printf("The reverse string is %s", str);
return 0;
}
void reversestring(char string[], int start, int end) {
printf("enter the string:\n");
scanf("%s", string);
int temp;
while (start < end) {
//printf("insidewhile\n");
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;
end --;
}
}
strlen()
can´t give you the length of a string, when its argument does not point to a valid string, which is the case on your example: strlen()
不能给你一个字符串的长度,当它的参数不指向一个有效的字符串时,你的例子就是这种情况:
char str[500];
int n;
n = strlen(str);
str
isn´t initialized with a string. str
不是用字符串初始化的。
Providing a pointer to a char
array which doesn´t contain a string as argument to strlen()
causes undefined behavior.提供指向不包含字符串的
char
数组的指针作为strlen()
参数会导致未定义的行为。
Also strlen()
doesn´t return an int
.另外
strlen()
不返回int
。 It´s return value is of type size_t
.它的返回值是
size_t
类型。
Also use fgets()
instead of scanf()
when input a string.输入字符串时也使用
fgets()
而不是scanf()
。 It is a little bit more safe.它更安全一点。
Solution:解决方案:
Let the string get entered in main()
into str
, then use strlen()
and thereafter call the reversestring()
function:让
main()
的字符串输入str
,然后使用strlen()
,然后调用reversestring()
函数:
char str[500];
size_t n;
printf("enter the string:\n");
fgets(str,500,stdin);
n = strlen(str);
reversestring(str, 0, n-1);
I also edited the function declaration and the printf()
accordingly to take care of the size_t
type.我还相应地编辑了函数声明和
printf()
以处理size_t
类型。
Here is the full code ( Online Example ):这是完整的代码(在线示例):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reversestring(char string[], size_t start, size_t end);
int main()
{
char str[500];
size_t n;
printf("enter the string:\n");
fgets(str,500,stdin);
str[strcspn(str, "\n")] = 0; // removing trailing newline from fgets
n = strlen(str);
reversestring(str, 0, n-1);
printf("%zu\n", n);
printf("The reverse string is %s", str);
return 0;
}
void reversestring(char string[], size_t start, size_t end)
{
int temp;
while(start < end)
{ //printf("insidewhile\n");
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;
end --;
}
}
Output:输出:
enter the string:
helloworld
10
The reverse string is dlrowolleh
You should use strlen
after the initialization of str
and set a correct size of third argument of reversestring
:您应该在
str
初始化后使用strlen
并设置reversestring
的第三个参数的正确大小:
size_t max_length = 128;
reversestring(str, 0, max_length);
n = strlen(str);
You have no string in the declared character array声明的字符数组中没有字符串
{ char str[500];
int n;
The character array is not initailzied.字符数组未初始化。 So this call
所以这个电话
n = strlen(str);
results in undefined bahavior.导致未定义的行为。
This code snippet from the function reversestring这个来自函数 reversestring 的代码片段
printf("enter the string:\n");
scanf("%s", string);
shall be outside the function and used before the function is called.应在函数之外并在调用函数之前使用。
The function should be declared at least like该函数应至少声明为
char * reversestring( char string[], size_t n );
There is no need to declare the function with three parameters like you did不需要像你那样用三个参数声明函数
void reversestring(char string[], int start, int end);
because a call to this function can be substituted for the call of the previous shown function like因为对这个函数的调用可以代替之前显示的函数的调用,比如
reversestring( string + start, end - start + 1 );
Also pay attention to that using the format %s
does not allow to enter a sentence.还要注意使用格式
%s
不允许输入句子。 Moreover using this format specifier with the function scanf
is unsafe, And the function strlen
has the return type size_t
.此外,将此格式说明符与函数
scanf
是不安全的,并且函数strlen
具有返回类型size_t
。
Here is the function definition这是函数定义
char * reversestring( char s[], size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i -1];
s[n-i-1] = c;
}
return s;
}
Here is a demonstrative program.这是一个演示程序。
#include <stdio.h>
#include <string.h>
char * reversestring( char s[], size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i -1];
s[n-i-1] = c;
}
return s;
}
int main(void)
{
enum { N = 500 };
char s[N];
printf( "Enter a string: " );
fgets( s, N, stdin );
s[ strcspn( s, "\n" ) ] = '\0';
size_t n = strlen( s );
puts( reversestring( s, n ) );
char *p = strchr( s, ' ' );
if ( p != NULL && ( p = strchr( p + 1, ' ' ) ) != NULL )
{
reversestring( s, p - s );
puts( s );
}
return 0;
}
Its output might look for example like它的输出可能看起来像
Enter a string: Hello Shivam Gupta
atpuG mavihS olleH
Shivam Gupta olleH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.