荷兰国旗问题(重新排列由Red,White,Blue构成的数组)

要求重新排列一个由Red、White和Blue(这是荷兰国旗的颜色)构成的数组,使得所有的Red都排在最前面,White排在其次,Blue排在最后。

【输入格式】

一行多个字符串,每个字符串都是 "Red" "White" 或 "Blue"

【输出格式】

一行按要求将原来的多个字符串排序后输出。

【提示】

strtok()函数可从一个字符串中提取以特殊字符分隔的子串。

#include<cstring>

  char inStr[256];
  cin.getline(inStr,256);
  
  char* pch;
  pch = strtok (str," ,.-");
  while(pch != NULL)
  {
    cout << pch;
    pch = strtok (NULL," ,.-");
  }


#include<iostream>
#pragma warning(disable:4996)
#include<stdio.h>
using namespace std;
int main()
{
    char inStr[256];
    cin.getline(inStr, 256);
    char res[768] = {};
    char red[256] = { 0 };
    char white[256] = { 0 };
    char blue[256] = { 0 };
    char* pch;
    pch = strtok(inStr, " ,.-");
    while (pch != NULL)
    {
        if (strcmp(pch, "Red") == 0) {
            sprintf(red, "%s%s", red, pch);
            pch = strtok(NULL, " ,.-");
            continue;
        }
        if (strcmp(pch, "White") == 0) {
            sprintf(white, "%s%s", white, pch);
            pch = strtok(NULL, " ,.-");
            continue;
        }
        if (strcmp(pch, "Blue") == 0) {
            sprintf(blue, "%s%s", blue, pch);
            pch = strtok(NULL, " ,.-");
            continue;
        }
        
    }
    sprintf(res, "%s%s%s", red, white, blue);
    cout << res;
    

}

img