游客 Signup | Login
中文 | En

3722 - C++字符串作业1:编写重载函数来打印字符串

编写函数 print_spaced 来打印字符串,要求打印出的字符串每个字母之间都有一个空格。要求编写两个同名函数,一个支持字符数组输入,另一个支持string类型输入。然后编写main函数测试这两个函数,第一个使用字符数组输入,第二个使用string类型输入。

函数原型为:void print_spaced(char s[]) ;   void print_spaced(string s) ;

Input

两个字符串,长度不超过100,只包含英文大小写字母,不含其他字符。

Output

经间隔空格处理后的两个字符串,两个字符串分居两行。注意字符串的最后一个字母后面没有空格。

Examples

Input

news
final

Output

n e w s
f i n a l

Hint

判断字符串是否结束的方法:

若用字符数组,例如char s1[101];int i=0;   当s1[i]==‘\0’   时结束。因此循环条件是while (s[i])或 while (s[i]!='\0') 

若用string类,l例如string 上; 还可用for(i=0,i<s2.length(),i++)

Solution C++

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
void print_spaced(char s[]){
	int i;
	while(s[i]!='\0'){
		cout<<s[i];
		if(i<strlen(s)-1){
			cout<<' ';
		}
		i++;
	}
}
void print_spaced(string s){
	int i;
	for(i=0;i<s.length();i++){
		cout<<s[i];
		if(i<s.length()-1){
			cout<<' ';
		}
	}
}
main(){
	char s1[101];
	string s2;
	cin.getline(s1,101);
	cin>>s2;
	print_spaced(s1);
	cout<<endl;
	print_spaced(s2);
}

Hint

判断字符串是否结束的方法:

若用字符数组,例如char s1[101];int i=0;   当s1[i]==‘\0’   时结束。因此循环条件是while (s[i])或 while (s[i]!='\0') 

若用string类,l例如string 上; 还可用for(i=0,i<s2.length(),i++)

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题