1、trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了,例子如下:String s=" Hello World ".trim();就是把"Hello World"放入s中。
专注于为中小企业提供网站制作、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业通化免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
2、例程:
void trim(char* s, char c)
{
char *t = s;
while (*s == c){s++;};
if (*s)
{
char* t1 = s;
while (*s){s++;};
s--;
while (*s == c){s--;};
while (t1 = s)
{
*(t++) = *(t1++);
}
}
*t = 0;
}
int main()
{
char mm[] = "**hello word!**";
trim(mm, '*');
printf("%s\n", mm);
}
c语言去掉字符串的空格函数 void trim(char *s){} 如下:
#include stdio.h
void trim(char *s){
int i,L;
L=strlen(s);
for (i=L-1;i=0;i--) if (s[i]==' ')strcpy(s+i,s+i+1);
}
int main(){
char s[100];
printf("input 1 line string\n");
gets(s);
trim(s);
printf("%s\n",s);
return 0;
}
例如:
input 1 line string
abc 123 XYZ |
输出:abc123XYZ|
#includestdio.h
#define M 1024
void main() { int i,j,n,nn,a[M],b;
scanf("%d",n); if ( nM ) n=M;
for ( i=0;in;i++ ) scanf("%d",a[i]);
for ( i=nn=1;in;i++ ) {
for ( j=b=0;jnn;j++ ) if ( a[i]==a[j] ) { b=1;break; }
if ( b==0 ) { a[nn]=a[i]; nn++; }
}
for ( i=0;inn;i++ ) printf("%d ",a[i]); printf("\n");
}
#include "stdafx.h"
#include string.h
#include stdio.h
#include stdlib.h
struct STUDENT{
int Number;
char Name[32];
int Class;
float Score;
};
#define IS_WHITE(ch) ((unsigned char)ch=15 || ch==' ')
//trim函数去除字符串str前后的空白字符
char *trim(char *str)
{
char ch;
char *p, *q, *r;
p=str; q=str; r=str-1;
while(1)
{
ch=*p;
if (!IS_WHITE(ch)) break;
if (ch==0) break;
p++;
}
while(1)
{
ch=*p++;
*q=ch;
if (ch==0) break;
if (!IS_WHITE(ch)) r=q;
q++;
}
*++r=0;
return str;
}
int InputStudent(const char *filename)
{
FILE *f;
int n;
char str[200];
struct STUDENT student;
f=fopen(filename,"wb");
if (f==NULL) return 0; //返回0表示失败
for (n=0; ; ++n)
{
printf("请输入学号: "); scanf("%d", student.Number);
if (student.Number==0) break;
while(1)
{
printf("请输入姓名:"); gets(str);
trim(str);
if (str[0]==0) continue; //确保姓名不为空
strncpy(student.Name, str, sizeof(student.Name)-1);
//上句确保输入姓名长度不超过31字节
break;
}
printf("请输入班级:\n"); scanf("%d", student.Class);
printf("请输入成绩:\n"); scanf("%f", student.Score);
fwrite(student, sizeof(student),1, f);
// printf("%08d %-32s %-3d %f\n",
// student.Number,
// student.Name,
// student.Class,
// student.Score);
}
fclose(f);
return n; //返回有多少个学生信息录入
}
void SortStudent(struct STUDENT *student, int n)
{
int i,j;
struct STUDENT *a, *b, t;
for (j=0; jn-1; ++j)
{
a=student+j;
for (i=j+1; in; ++i)
{
b=student+i;
if (a-Number=b-Number) continue; //否则交换
memcpy(t,a,sizeof(struct STUDENT));
memcpy(a,b,sizeof(struct STUDENT));
memcpy(b,t,sizeof(struct STUDENT));
}
}
}
void PrintStudent(const struct STUDENT *student, int n)
{
int i;
for (i=0; in; ++i, ++student)
printf("%08d %-32s %-3d %6.1f\n",
student-Number,
student-Name,
student-Class,
student-Score);
}
void main()
{
int n1, n2, n;
struct STUDENT *student;
const char fn1[]="d:\\student1.bin"; //文件1
const char fn2[]="d:\\student2.bin"; //文件2
const char fn[]="d:\\student.bin"; //合并后的文件
FILE *f;
printf("学生信息录入(录入过程中,学号输入为0,则退出录入)\n");
printf("第一步:%s中的学生信息录入:\n", fn1);
n1=InputStudent(fn1);
if (0==n1)
{
printf("%s中的学生信息录入有误,退出程序\n",fn1);
return;
}
printf("第二步:%s中的学生信息录入:\n", fn2);
n2=InputStudent(fn2);
if (0==n2)
{
printf("%s中的学生信息录入有误,退出程序\n",fn2);
return;
}
printf("第三步:合并%s, %s\n",fn1,fn2);
student=(struct STUDENT *)malloc((n1+n2)*sizeof(struct STUDENT));
f=fopen(fn1,"rb");
if (f==NULL) {printf("%s打开失败,退出程序\n",fn1); return;}
n=fread(student,sizeof(struct STUDENT), n1, f);
fclose(f);
if (n!=n1) {printf("%s读入错误,退出程序\n",fn1); return;}
f=fopen(fn2,"rb");
if (f==NULL) {printf("%s打开失败,退出程序\n",fn2); return;}
n=fread(student+n1,sizeof(struct STUDENT), n2, f);
// PrintStudent(student,n1+n2); //输出学生信息
fclose(f);
if (n!=n2) {printf("%s读入错误,退出程序\n",fn2); return;}
n=n1+n2;
SortStudent(student,n);
f=fopen(fn,"wb");
if (f==NULL) {printf("%s建立失败,退出程序\n",fn); return;}
fwrite(student, sizeof(struct STUDENT), n, f);
fclose(f);
PrintStudent(student,n); //输出学生信息
free(student);
}