C 更改字符串中某些单词的顺序
我已经试着解决这个问题一周了,但我的代码不起作用,我不明白为什么以及如何修改它。这项工作是: 从用户接收一个长度,然后接收一个长度为'length'的字符串(str),然后从用户接收一个数字(int n)。然后我需要执行函数'void reverseEnumWords(char*str,int n)。该函数反转字符串中的前n个字。例如:“我是你的父亲星球大战”和n=3: “我是你的父亲星球大战”。假设单词之间用“”分隔是正确的。谢谢你的帮助C 更改字符串中某些单词的顺序,c,string,pointers,malloc,calloc,C,String,Pointers,Malloc,Calloc,我已经试着解决这个问题一周了,但我的代码不起作用,我不明白为什么以及如何修改它。这项工作是: 从用户接收一个长度,然后接收一个长度为'length'的字符串(str),然后从用户接收一个数字(int n)。然后我需要执行函数'void reverseEnumWords(char*str,int n)。该函数反转字符串中的前n个字。例如:“我是你的父亲星球大战”和n=3: “我是你的父亲星球大战”。假设单词之间用“”分隔是正确的。谢谢你的帮助 #include <stdio.h> #i
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
void Reverse()
{
int len,num;
char *str;
printf("Please enter how many chars to allocate:\n");
//Asking from the user the length of a string.
scanf("%d", &len);
//Allocating memory for the string.
str = (char*)calloc(len, sizeof(int));
//Making sure the allocation was successful.
if (!str)
printf("Error: Cannot allocate Memory\n");
printf("Allocated %d chars\n", len);
printf("Please enter your string:\n");
scanf("%s", str);
printf("Please enter how many words to reverse:\n");
scanf("%d", &num);
ReverseNumWords(*str, num, len);
free(str);
}
void ReverseNumWords(char*str, int num,int len)
{
char *sub;
char temp;
//Allocating memory for the string.
sub = (char*)calloc(len, sizeof(int));
//Making sure the allocation was successful.
if (!sub)
printf("Error: Cannot allocate Memory\n");
int i, j,l;
i = j = 0;
for (; i < len, j <= num; i++)
if (str[i] == '\0' || str[i] == 0)
j++;
for (l = 0; l < i; l++)
sub[i] = str[i];
for (j = 0; j < i; j++)
temp = sub[j];
sub[j] = sub[i - (1+j)];
sub[i - (1+j)] = sub[j];
reverseWords(*sub);
}
void reverseWords(char *sub)
{
char *word_begin = sub;
char *temp = sub;
while (*temp)
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp - 1);
}
else if (*temp == ' ')
{
reverse(word_begin, temp - 1);
word_begin = temp + 1;
}
}
reverse(sub, temp - 1);
}
void reverse(char *begin, char*sub, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
printf("%s\n", sub);
}
#包括
#包括
#包括
#包括
无效反向()
{
int len,num;
char*str;
printf(“请输入要分配的字符数:\n”);
//询问用户字符串的长度。
scanf(“%d”和“len”);
//为字符串分配内存。
str=(char*)calloc(len,sizeof(int));
//确保分配成功。
如果(!str)
printf(“错误:无法分配内存\n”);
printf(“已分配%d个字符,\n”,len);
printf(“请输入您的字符串:\n”);
scanf(“%s”,str);
printf(“请输入要反转的字数:\n”);
scanf(“%d”和&num);
反向枚举单词(*str,num,len);
自由基(str);
}
void reverseEnumwords(char*str,int num,int len)
{
char*sub;
焦炭温度;
//为字符串分配内存。
sub=(char*)calloc(len,sizeof(int));
//确保分配成功。
如果(!sub)
printf(“错误:无法分配内存\n”);
int i,j,l;
i=j=0;
对于(;i
int len,num;
char *str;
printf("Please enter how many chars to allocate:\n");
//Asking from the user the length of a string.
scanf(" %d", &len);
//Allocating memory for the string.
str = (char*)malloc(sizeof(char)*(len+1));
//Making sure the allocation was successful.
if (!str)
printf("Error: Cannot allocate Memory\n");
printf("Allocated %d chars\n", len);
printf("Please enter your string:\n");
scanf(" %[^\n]s", str);
printf("Please enter how many words to reverse:\n");
scanf(" %d", &num);
ReverseNumWords(*str, num, len);
free(str);
因为当使用%s进行读取时,您将在找到的第一个“”(空格)处停止。并且您希望读取,直到找到一个\n(enter)。在此行中:
str = (char*)calloc(len, sizeof(int));
这需要:
str = (char*)calloc(len, sizeof(char));
我还建议您使用为字符串分配内存,因为您似乎在使用calloc()
时遇到了问题
您可以这样使用它:
char *str = malloc(len+1); /* Since sizeof(char) is 1, you don't need to include it */
这也带来了一个事实,那就是你
很高兴看到您检查分配的返回值
这总是一件好事
另一种读取字符串的方法:
不必使用scanf()
从stdin
读取字符串,您可以使用
char*fgets(char*str,int n,FILE*stream)
从输入流中读取一行,并将字节复制到char*str
,该字节的大小必须为n
字节,作为它可以占用的空间阈值
有关FGET的注意事项:
- 在缓冲区末尾追加
\n
字符。如果愿意,可以轻松删除
- 出现错误时,返回
NULL
。如果未读取任何字符,则仍会在末尾返回NULL
- 必须为缓冲区指定大小
n
- 从
stdin
或文件*
读取指定的流
下面是一个如何使用它从stdin
读取输入行的示例:
char buffer[100]; /* statically declared buffer */
printf("Enter a string: ");
fgets(buffer, 100, stdin); /* read line of input into buffer. Needs error checking */
您还可以使用和之类的函数来反转和连接字符串
下面是一个示例程序,它在一个字符串中包含n个单词:
输出:
Reversed string = olleH tahw a ylevol day
首先,为什么要使用sizeof(int)分配字符?你说得对!更改了它,代码仍然不起作用……我觉得有一种简单的方法来编写代码,相反,我把它变得更复杂……有什么提示吗?你能告诉我你会如何成功地解决它吗?我现在正在编写你的代码。我的第一个想法是改变你阅读它的方式,我会尝试阅读字符,然后不是字符串。等一下,plsIt出现了SEGFULT,我正在试图找出为什么在VisualStudio面前花费了数小时,但无法找出代码不起作用的原因……谢谢您的帮助,但它没有解决问题……其他函数呢?我需要它们吗?或者有没有一种更短的方法来解决此练习?是的,您的答案是肯定的mWords也不正确。为什么在fin\0或0时通过字符串将j加1?如果在此之后设置j=0?在ReverseEnumWords中的第一个for循环中变量j的函数是什么?哦,我不知道num>是否正确,但是你使用了j++并且在之后不再使用它。在无尽的几天之后,我成功地解决了这个练习,但我的代码更复杂“凌乱”和冗长。谢谢你的解释!我从你那里学到了很多。@Cbeginner没问题。
Enter how many chars to allocate: 50
Allocated 51 chars.
Please enter your string: Hello what a lovely day
Please enter how many words to reverse: 4
Reversed string = olleH tahw a ylevol day