3421 - 考试九 字符串分割

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

将一个字符串(长度不超过100)用";"分割成不同的字符串并输出。

题目输入

输入一个字符串之前用";"分隔;

题目输出

按行输出分割后的字串;

输入/输出样例

输入格式

hello;world;

输出格式

hello
world

C语言解答

int main(int argc, const char * argv[]) {
    
    char data[100];
    char tmp [100];
    int i = 0;
    int j = 0;
    int count = 0;
    char c = data[i];
    gets(data);
    while (c != '\0') {
        while(c != ';') {
            i++;
            count ++;
            c = data[i];
        }
        memcpy(tmp,&data[j],sizeof(char)*count);
        count = 0;
        printf("%s\n",tmp);
        i++;
        j = i;
        c = data[i];
    }
    
}

C++解答

#include<bits/stdc++.h>
using namespace std;
string a;
int main()
{
	cin>>a;
	for(int i=0;i<a.size();i++)
	{
		if(a[i]!=';')
		cout<<a[i];
		else cout<<endl;
	}
   return 0;
}