华为牛客网机试题:参数解析

在命令行输入如下命令:

xcopy /s c:\ d:\,

各个参数如下: 

参数1:命令字xcopy 

参数2:字符串/s

参数3:字符串c:\

参数4: 字符串d:\

请编写一个参数解析程序,实现将命令行各个参数解析出来。

// canshuhjiexi.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "string.h"

int _tmain(int argc, _TCHAR* argv[])
{
    char text[300] = { "\0" };
    char temp[300] = { "\0" };
    int length = 0;
    int cp = 0;
    int count = 0;
    gets_s(text);
    length = strlen(text);
    for (int i = 0; i < length; i++)
    {
        if (text[i] == '\"')
        {
            cp++;
        }
        else if (text[i] != ' ')
        {
            temp[i] = text[i];
        }
        else if (cp % 2 == 0)
        {
            temp[i] = '\n';
            count++;
        }
        else
        {
            temp[i] = ' ';
        }
    }
    printf("%d\n",count+1);
    printf(temp);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fredqrp/article/details/81222167