c-更改字符串中某些单词的顺序



我已经尝试解决这个练习一周了,但我的代码不起作用,我不明白为什么以及如何更改它。练习是:从用户接收一个长度,然后接收一个与"length"一样长的字符串(str),然后从用户接收数字(int n)。然后我需要执行函数'void ReverseNumWords(char*str,int n)。该函数反转字符串中的前n个单词。例如:对于"我是你的父亲星球大战"和n=3:"你是我的父亲星球大战"。假设这些单词用"分隔是正确的。谢谢你的帮助!!

#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 Memoryn");
printf("Allocated %d charsn", 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 Memoryn");
int i, j,l;
i = j = 0;
for (; i < len, j <= num; i++)
if (str[i] == '' || 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 == '')
{
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("%sn", sub);
}

您的读取函数应该类似于

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 Memoryn");
printf("Allocated %d charsn", 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(回车)。

在行中:

str = (char*)calloc(len, sizeof(int));

这需要:

str = (char*)calloc(len, sizeof(char));

我还建议使用malloc为字符串分配内存,因为您在使用calloc()时似乎遇到了问题。

你会这样使用它:

char *str = malloc(len+1); /* Since sizeof(char) is 1, you don't need to include it */

这也带来了一个事实,即您不需要强制转换malloc()的结果。

很高兴看到您检查分配的返回值总是一件好事。

另一种读取字符串的方法:

您可以使用fgets,而不是使用scanf()stdin读取字符串。

char *fgets(char *str, int n, FILE *stream)从输入流中读取一行,并将字节复制到char *str,必须将n字节的大小作为其可以占用的空间阈值。

关于fgets的注意事项:

  • 在缓冲区末尾追加n字符。如果你愿意,可以很容易地取下
  • 出现错误时,返回NULL。如果没有读取任何字符,则在末尾仍然返回NULL
  • 缓冲区的大小必须为n
  • 读取指定的流。来自stdinFILE *

以下是如何使用它从stdin:读取一行输入的示例

char buffer[100]; /* statically declared buffer */
printf("Enter a string: ");
fgets(buffer, 100, stdin); /* read line of input into buffer. Needs error checking */

您还可以使用strtokstrcat等函数来反转和连接字符串。

下面是一个在字符串中包含n个单词的示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *allocate_chars(int *len);
void number_of_words(int *num);
void read_strings(char *str, int len);
char *reversed_words(char *str, int len, int num);
char *reverse_word(char *word);
int main(void) {
int len, num;
char *str, *result;
str = allocate_chars(&len);
read_strings(str, len);
number_of_words(&num);
result = reversed_words(str, len, num);
printf("Reversed string = %sn", result);
free(result);
free(str);
return 0;
}
char *allocate_chars(int *len) {
char *str;
printf("Enter how many chars to allocate: ");
if (scanf("%d", len) != 1 || *len < 1) {
printf("Invalid length.n");
exit(EXIT_FAILURE);
}
getchar();
str = malloc(*len+1);
if (!str) {
printf("Cannot allocate %d bytes for string.n", *len+1);
exit(EXIT_FAILURE);
}
printf("Allocated %d chars.n", *len+1);
return str;
}
void number_of_words(int *num) {
printf("Please enter how many words to reverse: ");
if (scanf("%d", num) != 1 || *num < 0) {
printf("Invalid number.n");
exit(EXIT_FAILURE);
}
}
void read_strings(char *str, int len) {
int slen;
printf("Please enter your string: ");
if (fgets(str, len, stdin) == NULL) {
printf("Cannot create buffer.n");
exit(EXIT_FAILURE);
}
slen = strlen(str);
if (slen > 0) {
if (str[slen-1] == 'n') {
str[slen-1] = '';
} else {
printf("Entered string bigger than buffer size of %d bytes.n", len);
exit(EXIT_FAILURE);
}
}
if (!*str) {
printf("No string entered.n");
exit(EXIT_FAILURE);
}
}
char *reversed_words(char *str, int len, int num) {
char *reversed, *word, *reversed_word;
const char *delim = " ";
int count = 1;
reversed = malloc(len+1);
if (!reversed) {
printf("Cannot allocate %d bytes for string.n", len+1);
exit(EXIT_FAILURE);
}
*reversed = '';
word = strtok(str, delim);
while (word != NULL) {
if (count <= num) {
reversed_word = reverse_word(word);
strcat(reversed, reversed_word);
free(reversed_word);
count++;
} else {
strcat(reversed, word);
}
word = strtok(NULL, delim);
if (word != NULL) {
strcat(reversed, delim);
}
}
return reversed;
}
char *reverse_word(char *word) {
char *reverse;
int slen, str_count = 0, i;
slen = strlen(word);
reverse = malloc(slen+1);
if (!reverse) {
printf("Cannot allocate %d bytes for string.n", slen+1);
exit(EXIT_FAILURE);
}
for (i = slen-1; i >= 0; i--) {
reverse[str_count++] = word[i];
}
reverse[str_count] = '';
return reverse;
}

样本输入:

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

相关内容

  • 没有找到相关文章