一个字符数组,其中包含一些数据和空格及换行,现在想把其中的数据提取出来怎么做???

来源:百度知道 编辑:UC知道 时间:2024/07/07 10:47:57
C++语言
不能使用VC++
必须是标准C++语言库
分界有换行也有空格

for遍历数组,判断是不是''和'\n'
#include "stdafx.h"
#include "string.h"

int main(int argc, char* argv[])
{
char str[]="as df\ngh";
int len=strlen(str);
for(int i=0;i<len;i++)
{
if(str[i]==' '||str[i]=='\n')
{
for(int j=i;j<len;j++)
str[j]=str[j+1];
}
}
printf("%s\n",str);
return 0;
}

以什么分界?换行还是空格?或者两者都是?
=============================================
sscanf典型应用
遍历一下数组记下各个空格或换行的偏移量,
while(sscanf(p,"%d",&i)) p移动相应偏移量加1

#include <iostream>
using namespace std;

int main()
{
char p[]="hell o\n\t";
char* s=new char[sizeof(p)/sizeof(char)];
int i=0;
while(p[i])
{ //空格的转义字符是 \040
if(p[i]!='\040' && p[i]!='\