尝试在没有字符串函数 strrev 的情况下反转字符串但在 c 中不起作用
trying to reverse string without string function strrev but not working in c
#include <stdio.h>
#include <string.h>
void main(void)
{
char in[15], rev[15];
printf("Enter a word (upto 15 letters): ");
gets(in);
for (int i = 0, j = 15; i < strlen(in); i++, j--)
{
rev[i] = in[j];
}
puts(rev);
}
没有显示错误,只是无法正常工作。
我做错了什么?
编辑:无 strrev
对于根据 C 标准的初学者,不带参数的函数 main 应声明为
int main( void )
函数 gets
不安全且不受 C 标准支持。而是使用 scanf
或 fgets
.
函数strlen
是一个标准的C字符串函数。所以根据要求你可以不使用它。
您没有反转字符串。您正试图以相反的顺序将一个字符串复制到另一个字符串中。
程序可以这样看
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "", rev[N];
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
rev[n] = '[=11=]';
for ( size_t i = 0; i < n; i++ )
{
rev[n - i - 1] = in[i];
}
puts( rev );
}
如果您确实需要就地反转字符串,则程序可以按以下方式查看
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "";
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
for ( size_t i = 0; i < n / 2; i++ )
{
char c = in[i];
in[i] = in[n - i - 1];
in[n - i - 1] = c;
}
puts( in );
}
编辑:getline
不是标准 C,它只能被 POSIX 系统识别。另一种解决方案是使用适用于两种操作系统的 fgets
。我提供了两个示例。
正如其他人已经指出的,您犯了一些错误:
- 获取用户输入时的不安全做法。
- 始终从 15 开始,即使输入字符串的字符数较少。
我创建了一个动态分配的小示例,它可以处理超过 15 个字符并修复了 afore-mentioned 问题。关键点内嵌评论。
示例:getline - POSIX
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
// Idea from
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
read = getline(&line, &len, stdin);
if (read > 0)
{
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
// "read-1" because we start counting from 0.
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free (line); /* free memory allocated by getline */
free(rev_line);
return 0;
}
示例:fgets - C 标准
fgets
没有 return 读取的字符数,所以它必须与 strlen
链接来决定为反转字符串分配多少字符。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int main (int argc, char *argv[]) {
char line[LINE_MAX];
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
if (fgets(line, LINE_MAX, stdin) != NULL)
{
line[strcspn(line, "\n")] = '[=11=]'; //fgets() reads the \n character (that's when you press Enter).
read = strlen(line);
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free(rev_line);
return 0;
}
#include <stdio.h>
#include <string.h>
void main(void)
{
char in[15], rev[15];
printf("Enter a word (upto 15 letters): ");
gets(in);
for (int i = 0, j = 15; i < strlen(in); i++, j--)
{
rev[i] = in[j];
}
puts(rev);
}
没有显示错误,只是无法正常工作。 我做错了什么?
编辑:无 strrev
对于根据 C 标准的初学者,不带参数的函数 main 应声明为
int main( void )
函数 gets
不安全且不受 C 标准支持。而是使用 scanf
或 fgets
.
函数strlen
是一个标准的C字符串函数。所以根据要求你可以不使用它。
您没有反转字符串。您正试图以相反的顺序将一个字符串复制到另一个字符串中。
程序可以这样看
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "", rev[N];
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
rev[n] = '[=11=]';
for ( size_t i = 0; i < n; i++ )
{
rev[n - i - 1] = in[i];
}
puts( rev );
}
如果您确实需要就地反转字符串,则程序可以按以下方式查看
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "";
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
for ( size_t i = 0; i < n / 2; i++ )
{
char c = in[i];
in[i] = in[n - i - 1];
in[n - i - 1] = c;
}
puts( in );
}
编辑:getline
不是标准 C,它只能被 POSIX 系统识别。另一种解决方案是使用适用于两种操作系统的 fgets
。我提供了两个示例。
正如其他人已经指出的,您犯了一些错误:
- 获取用户输入时的不安全做法。
- 始终从 15 开始,即使输入字符串的字符数较少。
我创建了一个动态分配的小示例,它可以处理超过 15 个字符并修复了 afore-mentioned 问题。关键点内嵌评论。
示例:getline - POSIX
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
// Idea from
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
read = getline(&line, &len, stdin);
if (read > 0)
{
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
// "read-1" because we start counting from 0.
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free (line); /* free memory allocated by getline */
free(rev_line);
return 0;
}
示例:fgets - C 标准
fgets
没有 return 读取的字符数,所以它必须与 strlen
链接来决定为反转字符串分配多少字符。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int main (int argc, char *argv[]) {
char line[LINE_MAX];
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
if (fgets(line, LINE_MAX, stdin) != NULL)
{
line[strcspn(line, "\n")] = '[=11=]'; //fgets() reads the \n character (that's when you press Enter).
read = strlen(line);
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free(rev_line);
return 0;
}