如何把一个字符串中指定的字符输出

bluenight 2003-08-30 12:54:22
如何把一个字符串中指定的字符输出,如abcdefg,我想只输出abc,如何做?
...全文
201 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
keiven 2003-08-31
  • 打赏
  • 举报
回复
用一个循环就搞定!
caoyun 2003-08-30
  • 打赏
  • 举报
回复
看这个`~http://student.zjzk.cn/course_ware/data_structure/web/chuan/chuan4.2.3.2.htm
xgp1226 2003-08-30
  • 打赏
  • 举报
回复
用字符指针循环查找!
danielhf 2003-08-30
  • 打赏
  • 举报
回复
#include <iostream> //接受字符串,并由用户指定从第几个字符开始,显示指定长度的字串

using std::cout;
using std::cin;
using std::endl;

int const size=50; //size of buffer

int main(int argc, char* argv[])
{
char buff[size]; //接受一个字串
cout<<"Enter a string: ";
cin.get(buff, size);

int beg; //从第几个字符开始显示?
cout<<"where to start: ";
cin>>beg;

int num; //要显示多少个字符?
cout<<"How many digits for output: ";
cin>>num;

cout<<"output string is: "; //输出所要求的字串
for(int i=beg-1; i<num+(beg-1); i++) //让数组从1开始
cout<<buff[i];

cout<<endl;
cout<<endl;
system("pause");
return 0;
}


chenzhiqiang 2003-08-30
  • 打赏
  • 举报
回复
char * str1="abcdefg";
char buf[80];

strncpy(buf,str1,3);
buf[3]=0;

如果是只想从第二个字符开始输出4个:
strncpy(buf,str1+1,4);
buf[4]=0
  • 打赏
  • 举报
回复
楼主方法果然独到,佩服
sandrowjw 2003-08-30
  • 打赏
  • 举报
回复
btw:对常量字符串不要用后面的办法。
sandrowjw 2003-08-30
  • 打赏
  • 举报
回复
用strtok(),
或者你把d改成0,输出以后再把d加回去。
1cs1ak1 2003-08-30
  • 打赏
  • 举报
回复



控制输出啊。
char *p = "abcdefg";
while(*p++ == 'a' || *p++ == 'b' || *p++ == 'c')
{
printf("%c",*(p-1));
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧