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

Lak*_*hmi 0 c++

我是新手程序员C/C++.学习指针和角色指针,并试图解决运动问题.问题陈述

给出一句"世界是一个住在的好地方"

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

输出应该是"A是生活世界的好地方"

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

#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;
    }
Run Code Online (Sandbox Code Playgroud)

不使用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;


}
Run Code Online (Sandbox Code Playgroud)

Kon*_*lph 9

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

基本上有三个步骤:

  1. 将句子分解为一个单词数组.
  2. 按长度排序单词数组.
  3. 输出单词,以空格分隔.

这些任务是用C++琐碎且不需要指针或类似的东西(这是一个很好的事情.指针有自己的位置在C,但只有很少的C++).

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

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

std::string word;
while (cin >> word)
    words.push_back(word);
Run Code Online (Sandbox Code Playgroud)

这将从标准输入中读取单个单词并将其存储在向量中.

第二步 能够 应该使用C++标准库sort函数来解决.

第三步只是迭代向量并将单词推送到cout流中.

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

如果你不想使用一个string类,你的第一步应该是写一个.它不一定是花哨的,但它至少应该处理处理字符串内存,读取输入和写入输出的基本机制.

  • @Beta但订单错了.首先学习高级工具,*然后*他们的小肠是如何工作的.毕竟,学龄儿童不是从佩诺诺的数学公理或生物学中的量子力学开始的.他们从高级流程开始,在扩展和深化他们的知识之前易于理解和应用. (5认同)
  • 我同意将任务分解为较小的任务,但如果要了解指针和数组,那么OP应该*不*使用`vector`.每个学生都应学会处理旧工具,然后才能进入新工具. (3认同)
  • 一般方法+1.他指出他不能使用`std :: string`,但分解的基本原则仍然适用. (2认同)
  • @Beta"每个学生都应该学习处理旧工具,然后再推进新工具" - 抱歉,这是错误的学习方法.我知道很多人声称学习必须自下而上,但这些人对教学法一无所知. (2认同)