永发信息网

vc++ 去空格

答案:3  悬赏:20  手机版
解决时间 2021-01-27 13:58
  • 提问者网友:留有余香
  • 2021-01-27 03:31
我的两个文件:
Point.h

#include<iostream.h>
#include <string.h>
class Point
{
public :
Point(int xx=0,int yy=0)
{
x=xx,y=yy;countP++;
}
Point(Point &p);
char * Trim(char *source)
{
char *p, *q;
p = q =source;
while(q)
{
if(*q!=' ')
*p++=*q;
q++;
}
return p;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
static void GetC()
{
cout<<" Object id ="<<countP<<endl;
}
static int countP;
private:
int x,y;

};

Point.cpp

#include "Point.h"
Point::Point(Point &p)
{
x=p.x;
y=p.y;
countP++;
cout<<"the point app is used"<<endl;
}
int Point ::countP=0;
void main()
{
char test[]=" 2345";
int len=strlen(test);
cout<<"the length is "<<len<<endl;
}
----------初学者 多多帮助---------------
我就是想把空格去掉,上面的Trim函数是在网上找的,不知道对不对!!在main里我怎样可以使用啊!!给我写一个调用的代码 谢谢!
最佳答案
  • 五星知识达人网友:白昼之月
  • 2021-01-27 05:06
while(q)
{
if(*q!=' ')
*p++=*q;
q++;
}
这里改成
while(q && *q!='\n');
{
if(*q!=' ')
*p++=*q;
++q;
}
*p = '\n';

main里面这样用
void main()
{
char test[]="1 2 3 45 6";
char *noblanks = Trim(test);
cout<<"after triming :"<<noblanks<<endl;
}
全部回答
  • 1楼网友:刀戟声无边
  • 2021-01-27 06:37
给你一个简单好用的trim函数吧: void trim(string & word) { size_t start = word.find_first_not_of("\r\n \t"); size_t end = word.find_last_not_of("\r\n \t"); if (start == string::npos) { // empty string word = ""; return; } else word = word.substr(start,end-start+1); }
  • 2楼网友:拜訪者
  • 2021-01-27 05:23

#include <stdio.h>

void main() {  char s[10] = {null};  register int i;  gets(s);  for (i=0;i<10;i++)  {   if (s[i]>'a' && s[i] < 'z')   {    s[i] = s[i] + 32;    printf("%c ",s[i]);   }  }  printf("\n"); }//vc6.0编译通过

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯