c++ - 根据单词长度对句子进行排序

标签 c++

我是 C/C++ 新手程序员。正在学习指针和字符指针,并试图解决一个练习题。问题陈述

给定一句话“World is a great place to live in”

问题描述:根据单词长度的升序重新排列给定的句子

输出应该是“A is in to live world great place”

我正在尽力并粘贴我编写的代码,但无法得出答案。有人可以指出错误。我相信有很多。

#include <iostream>    
#include <string.h>

using namespace std;
char* breakIntoWords(char *,char*pt);

    int getwordcount(char* p)
    {
     int wc = 0;

     while(*p == ' ')
      {
         p++;
      }
      while( 1) {

      while( *p != ' ' && *p != '\0' && *p !='\n')
      { 
        p++;

      }
    if(*p ==  ' ')
    {
       p++;
    }
    else{
       break;
    }


      wc++;
      }
    wc++;
      return wc;

    }

    int main()
    {

       char bsentence[120][5];
       char sentence[120];
        cout<<"Ent&er STring"<<endl;
        char *p ;
        p = "Test it again and welcome";
        strcpy(sentence, p);
        int wordcount =0;
        wordcount=getwordcount(sentence);
        char *pt = sentence;
        for(int i =0; i <wordcount; i++)
        {

         pt = breakIntoWords(pt,bsentence[i]);
        }



           for(int i =0; i< wordcount; i++)
        {
          for(int j=i; j<i;  j++)
           {
              int one = strlen(bsentence[i]);
              int two = strlen(bsentence[i+1]);

            if(one > two)
               {
                   char temp[120];
                   strcpy(temp,bsentence[i]);
                   strcpy(bsentence[i+1],temp);


           }

            }

        }
    char sen2[12];
     for(int i=0; i<wordcount; i++)
      {

          strcat(sen2,bsentence[i++]);
          strcat(sen2, " ");
       }
       strcpy(sentence,sen2);




        cout<<sentence;

    }
    char* breakIntoWords(char*p, char*pt)
    {
       int i =  0, j = 0;
      while( *p != ' ')
      {
       pt[i] = *p++;
       i++;

      }
      p++;
      pt[i]='\0';

      return p;
    }

不使用 String 类。

终于解决了。欢迎任何改进它的意见。

    #include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_WORD_SIZE 30
#define MAX_LINE_SIZE 100
using namespace std;

int getWordCount(char* s)
{
   int wc = 0;
   while(*s == ' ')
     s++;
   while(*s != '\n')
   {
      while(*s != ' ' && *s != '\n')
      {
         s++;
       }

       if(*s == ' ')
       {
         while(*s == ' ')
            s++;
       }
       wc++;
   }
   return wc;
}
char* getWord(char* Source, char* word)
{
     while(*Source == ' ')
         Source++;
      int i =0;
      while(*Source != ' ' && *Source != '\n')
      {
         word[i] = *Source;
         Source++;i++;
       }
       word[i]='\0';
       if(*Source == ' ')
       {
         while(*Source == ' ')
            Source++;
       }
       return Source;


}
void sortSentence(char* p[], int wc)
{
   char *temp = new char[MAX_WORD_SIZE];
   for(int i =0; i<wc; i++)
   {
      for(int j = i; j< (wc-1); j++)
       {
           if(strlen(p[j]) > strlen(p[j+1]))
            {
                strcpy(temp,p[j]);
                strcpy(p[j],p[j+1]);
                strcpy(p[j+1],temp); 
             }
        }
    }
    delete[] temp;
}
int main()
{

 char* string;
 string = new char[MAX_LINE_SIZE];
 cout<<"Enter Sentence"<<endl;
 fgets(string,MAX_LINE_SIZE,stdin);
 int wordCount = getWordCount(string);
 char* pArrayOfPointers[30];
 char* tempString = string;
 for(int i =0; i< wordCount; i++)
 {
    char *ptemp;
      ptemp =new char[MAX_WORD_SIZE];
    pArrayOfPointers[i]= ptemp;
    tempString = getWord(tempString,pArrayOfPointers[i]);
    cout<<pArrayOfPointers[i]<<endl;          
  }
  sortSentence(pArrayOfPointers, wordCount);

  strcpy(string,pArrayOfPointers[0]);
  strcat(string," ");
  for(int i =1; i< wordCount; i++)
  {
     strcat(string,pArrayOfPointers[i]);
     strcat(string," ");

   }
   cout<<string;
 delete[] string;


}

最佳答案

您的代码比必要的复杂得多,因为您没有将问题分解为更容易解决的更小的任务。

基本上分为三个步骤:

  1. 将句子分解成单词数组。
  2. 按长度对单词数组进行排序。
  3. 输出单词,以空格分隔。

这些任务中的每一个在 C++ 中都是微不足道的,并且不需要指针或类似的东西(这是一件的事情。指针在 C 中占有一席之地,但在 C++ 中很少见)。

例如,第一步可以使用 C++ IO 流和 vector 容器来解决:

std::vector<std::string> words;

std::string word;
while (cin >> word)
    words.push_back(word);

这从标准输入中读取单个单词并将它们存储在一个 vector 中。

第二步可以应该使用C++标准库解决sort功能。

第三步只是迭代 vector 并将单词推送到 cout 流。

总而言之,这不应超过 15 行代码。

如果您不想使用 string 类,您的第一步应该是编写一个。它不必很花哨,但它至少应该处理处理字符串内存、从输入读取和写入输出的基 native 制。

关于c++ - 根据单词长度对句子进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052089/

相关文章:

c++ - 使用智能指针制作多态容器的正确方法是什么?

请提供 C++ 线程和字符串帮助

c++ - 如何使用 bjam 构建 boost 示例?

c++ - 交换两个对象的两个属性的值

c++ - 派生类比基类大,即使它由引用变量组成

c++ - C++编译错误已自行修复(试图找出原因)

c++ - 我们应该如何解释带有嵌入式逗号的宏

c++ - 用于整数下限和上限查询的快速数据结构?

c++ - 软件模型设计

c++ - 使用 C 与使用 C++ 实现神经网络?