C语言输入一个字符串,输出第一个字符和最后一个字符
输入字符串会整,怎么输出第一个字符和最后一个字符啊
得到字符串的长度,然后减去1作为下标就是最后一个字符
#include <iostream>
using namespace std;
#include <string>
int main()
{
char s[1000];
cin>>s;
int len = strlen(s);
cout<<"首字符是:"<<s[0]<<endl;
cout<<"尾字符是:"<<s[len-1]<<endl;
}
比如输入的字符串保存在str[]数组内
输出时候可以输出str[0]和str[strlen(str)-1]
如果输入字符串长度随机,就存在*str里