【问题标题】:a program to reverse each word in a string( ive read the previous solutions but im looking for one using the functions i only here [closed]一个反转字符串中每个单词的程序(我阅读了以前的解决方案,但我正在寻找一个使用我只在这里的函数的解决方案[关闭]
【发布时间】:2015-01-18 10:46:10
【问题描述】:

我已经编写了这个 c 程序,但我总是收到与输出相同的输入句子而没有任何变化!我已经拆分了字符串中的每个单词,然后颠倒了它们的位置,但效果不佳!请任何解决方案!

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main ()
{    
    char A[81][81];
    int t=0,j=1,k=0,l;
    puts("Input a sentence (max 80 character)");
    gets(A[0]);
    while (A[0][t]!='\0')
    {
        if(A[0][t]=='\32')
        {
            j++;
            t++;
            k=0;
        }
        A[j][k]=A[0][t];
        k++;
        t++;
    }
    for (l=j;l>0;l--)
    {
        printf("%s",A[l]);
    }
    getch();
}

【问题讨论】:

  • 请更明确地说明输入和预期输出。
  • 例如!如果我输入“萨米到达房子”,则需要输出“到达萨米的房子”,但输出作为输入给出,没有任何单词的反转
  • 我划分了字符串中的每个单词并将每个单词放在一个数组中,我认为用户可以输入一个字母而不是每个单词,因此可能需要更多的数组来划分这些字母谢谢
  • 有人对您的个人资料照片有疑问:meta.stackoverflow.com/questions/283942/…
  • 哈哈没关系!没关系

标签: c string reverse words


【解决方案1】:
#include <stdio.h>
#include <string.h>
#include <conio.h>

int main(void){ 
    char A[81][81] = {0};
    int t=0,j=1,k=0,l;
    puts("Input a sentence (max 80 character)");
    scanf("%80[^\n]", A[0]);//'gets' has already been abolished, it should explore a different way.
    while (A[0][t] != '\0'){
        if(A[0][t] == ' '){
            ++j;
            k=0;
            while(A[0][t] == ' ')//Skip spaces
                ++t;
        } else {
            A[j][k++] = A[0][t++];
        }
    }
    for (l=j;l>0;l--){
        printf(" %s",A[l]);
    }
    puts("");
    getch();
}

【讨论】:

  • 请稍微注释一下代码。
  • 由于程序的结构几乎相同,所以更改部分有问题。哈哈。这是按照 OP 的意图重写的。
  • 有些人似乎相信这是令人难以置信的低能力人。
  • 感谢您的帮助!!
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 2022-08-15
  • 2022-06-12
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
相关资源
最近更新 更多