求解析下面的程序#include "stdafx.h"#include "iostream"#include "string"using namespace std;string enter(string);string trim(string);string l_trim(string);string r_trim(string);int main(){ string strEnter; strEnter = enter("Please enter
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/26 05:28:53
求解析下面的程序#include "stdafx.h"#include "iostream"#include "string"using namespace std;string enter(string);string trim(string);string l_trim(string);string r_trim(string);int main(){ string strEnter; strEnter = enter("Please enter
求解析下面的程序
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
string enter(string);
string trim(string);
string l_trim(string);
string r_trim(string);
int main()
{
string strEnter;
strEnter = enter("Please enter your name: ");
strEnter = trim(strEnter);
cout
求解析下面的程序#include "stdafx.h"#include "iostream"#include "string"using namespace std;string enter(string);string trim(string);string l_trim(string);string r_trim(string);int main(){ string strEnter; strEnter = enter("Please enter
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
string enter(string);
string trim(string);
string l_trim(string);
string r_trim(string);
int main()
{
string strEnter;
strEnter = enter("Please enter your name: ");
strEnter = trim(strEnter);
cout<<"Hello, "<<strEnter<<"!\n";
return 0;
}
string enter(string hint) //返回值,参数是string类型
{
string s;
cout<<hint;
getline(cin, s); //获取一行,不需要指定大小
return s; //返回s
}
string l_trim(string s)
{
int i = 0, j;
char c[100];
for (i = 0; s[i] != 0 && (s[i] == '\t' || s[i] == ' '); i++);
//当s字符串的某一个字符不等于0,而且 s字符串 等于 \t 或者 s字符串等于空格,条件都满足才成立
for (j = 0; s[i] != 0; j++)
c[j] = s[i++];
//把s字符串单个复制到c里面,结束条件等于0
c[j] = 0;
return c;
}
string r_trim(string s)
{
int i = s.length(); //获取s字符串的长度
char c[100];
for (i--; i > 0 && (s[i] == '\t' || s[i] == ' '); i--);
//递减循环
c[++i] = 0;
for (i--; i >= 0; i--)
c[i] = s[i];
return c;
}
string trim(string s)
{
int i = 0, j, n;
char c[100];
s = l_trim(r_trim(s)); //调用r_trim函数
n = s.length();
while (i <= n)
{
c[i] = s[i] | 32; //转换成其他的数值 ,加入s[i] = a ,a|32 就是a转换成ASCII码十进制值,然后再转换2进制,32也如此,返回值给c的字符
i++;
}
c[--i] = 0;
for (i=1; i < n; i++)
{
if (c[i] == ' ' && c[i + 1] == ' ')
{
for (j = i + 1; j <= n; j++)
c[j] = c[j + 1];
i--;
n--;
}
}
c[0] = c[0] & 223; //转换的意思
for (i = 1; i < n; i++)
if (c[i] == ' ')
c[i + 1] = c[i + 1] & 223;
return c;
}