用char数组如何写啊与string 有什么区别

从键盘输入若干行英文句子,自定义输入的结束方式;按句子的长度从短到长,输出已输入的各句子。(要求分别实现两个不同的版本:(1)使用C风格的字符串,即char数组或char*; (2)使用C++风格的字符串,即string对象)
使用string 对象:

用char数组或char*;

#include<iostream>
#include<cstring>
using namespace std;

void sort(char *a[],int n)
{
    int i,j;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
            if(strlen(a[j]) > strlen(a[j+1]))
            {
                char *t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
}

int main()
{
    char *a[50], *t;
    int len=0,i;
    while (true)
    {
          t=(char *)malloc(sizeof(char)*10);  //使用动态内存分配给字符串指针分配空间
        cin.getline(t,100);
        if (strcmp(t,"#")==0) //输入 # 表示输入结束
            break;
        a[len++] = t;
    }
    sort(a, len);
    for(i=0;i<len;i++)
        cout << a[i] << endl;
}

img

如有帮助,望采纳!谢谢!