简体   繁体   English

我的代码没有正确反转每个单独单词的字符串?

[英]My code is not reversing string of characters from each separate word correctly?

I am doing an assignment where I have to implement a function reverse() that reverses the string between indexes i and j inclusively.我正在做一个任务,我必须实现一个函数 reverse() 来反转索引 i 和 j 之间的字符串。 reverse(str, 0, 4) will reverses the string between indexes of 0 and 4 from apple to elppa.All words can be reversed by finding start and end positions of each word and reversing the word. reverse(str, 0, 4) 将索引 0 和 4 之间的字符串从 apple 反转为 elppa。通过查找每个单词的开始和结束位置并反转单词,可以反转所有单词。

void reverse(char *str, int i, int j). for example

userinput:用户输入:

apple orange banana

will output:将输出:

elppa egnaro ananab

but my code prints nothing.但我的代码什么也没打印。 I know thereare a lot of bugs but I am stuck and don't know how to go from here.我知道有很多错误,但我被卡住了,不知道如何从这里开始。 My code is:我的代码是:

#include <stdio.h>
#include <string.h>
void reverse(char *str, int i, int j);
int i, j, k, len, ind, temp;
char str[100];
int main()
{

    printf("Enter a string: \n");
    gets(str);
    len = strlen(str);
    reverse(str, i, j);
    return 0;
}
void reverse(char *str, int i, int j)
{
    for (i = 0; i < len; i++)//
    {
        ind = i;
            //index of the end of each word
        if (str[i] == ' ' || str[i] == '\n')//ends at index of whites space
        {
            ind = i - 1;//index of empty space -1
            i= 0;//i is reset to 0
            if (i <= ind)//0<last index of word
            {
                //reverse
                temp = str[i];
                str[i] = str[ind];
                str[ind] = temp;
            }
        }
      }
   printf("%s\n",str[i]);
}

Sorry, but your code is really broken.对不起,但你的代码真的坏了。 I don't really know how to "fix" it without basically rewriting it from scratch.我真的不知道如何在不从头开始重写它的情况下“修复”它。 The first most obvious is this:第一个最明显的是:

void reverse(char *str, int i, int j)
{
    for (i = 0; i < len; i++)

You're throwing away whatever was in the variable i by initializing it to zero.您通过将变量i初始化为零来丢弃变量i中的任何内容。 So this function will do the same thing irregardless of the value if i .因此,无论i的值如何,此函数都会执行相同的操作。

You are also using a lot of global variables for no good reason.您还无缘无故地使用了许多全局变量。 NEVER use globals unless you have a good reason, and this is not one of them.除非你有充分的理由,否则永远不要使用全局变量,这不是其中之一。 Move the declaration of i, j, k, len, ind and temp inside the body of reverse.将 i、j、k、len、ind 和 temp 的声明移动到 reverse 的主体内。 Move declaration of str inside main.在 main 中移动 str 的声明。

The variable j is never used.从不使用变量 j。

NEVER use the function gets .永远不要使用函数gets It has been removed from modern C.它已从现代 C 中删除。

printf("%s\\n",str[i]); makes no sense.没有意义。 You're trying to print the i:th character, but as a string.您正在尝试打印第 i:th 个字符,但作为字符串。 This is undefined behavior.这是未定义的行为。

Turn on compiler warnings.打开编译器警告。 This is what I got when I compiled your code:这是我编译你的代码时得到的:

$ gcc b.c -Wall -Wextra
b.c: In function ‘main’:
b.c:10:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
     gets(str);
     ^~~~
     fgets
b.c: In function ‘reverse’:
b.c:34:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
    printf("%s\n",str[i]);
            ~^    ~~~~~~
            %d
b.c:15:36: warning: unused parameter ‘j’ [-Wunused-parameter]
 void reverse(char *str, int i, int j)
                                ~~~~^

The first programming principle to apply here is decomposition .这里应用的第一个编程原则是分解 You have two tasks: identify the words in the input string, and reverse the letters of each word.您有两个任务:识别输入字符串中的单词,并反转每个单词的字母。 Trying to do both together is much more difficult than doing either one.尝试同时进行两者比做任何一个都困难得多。

First write a function that will reverse one word, given its start and end indexes, then write a second function that will identify the words and call the first function to reverse them.首先编写一个函数来反转一个单词,给定它的开始和结束索引,然后编写第二个函数来识别单词并调用第一个函数来反转它们。 You can finish and test the first function before you attempt the second.您可以在尝试第二个功能之前完成并测试第一个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM