码迷,mamicode.com
首页 > 其他好文 > 详细

对于特殊字符串的处理方法

时间:2015-09-05 15:00:58      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

对于特殊的字符串,我们对字符串进行特殊与非特殊两种,第一种字符串开头特殊字符提到末尾处理方法或者末尾字符串提到开头,总之先处理特殊的就OK了

开头提到末尾处理:

#include "stdio.h"
#include"stdlib.h"
#include "conio.h"
void fun(char*a)
{
int i=0,j=0;
char *p=a;
while(*p&&*p==‘*‘) //单独处理字符串前的*字符,并保存到a指针里去
{ a[i++]=*p;
p++;
}
while(*p)
{
a[j++]=*p;
p++;
a[i++]=a[j]; //开始衔接 
}

a[i]=‘\0‘; // 加上结束标志
}
main()
{
char s[80];
printf("printf the string :\n");
gets(s);
fun(s);
printf("The string after deleted %c :\n");
puts(s);
}

 

 

结果截图:

技术分享

  末尾提到开头处理:
 通过一个for循环直接指向字符串的最后一个字符\0,再减1指向最后一个了非空字符

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "conio.h"
void fun(char *a)

char *p;

*p=a;

int i=0,j=0;
{ while(*a!=‘\0‘)
a++;
a--;
//直接指向字符串末尾
while(*a==‘*‘)

{

a[i++];  //把末尾的*保存在a[i],a[j]保存删除后的字符串,最后衔接起来

a[j--];

}

 

while(a[i]!=‘\0‘)

a[j++]=a[j]

// 长度加1加结束符
a[j]=‘\0‘;
}

void main()
{char s[100];
printf("Enter the string :\n");
gets(s);
fun(s);
printf("The string after deleted :\n");
puts(s);


}

/* Note:Your choice is C IDE */#include "stdio.h"#include "conio.h"void fun(char *a){ while(*a!=‘\0‘)a++;a--;//直接指向字符串末尾while(*a==‘*‘)a--;  //直接删除存储空间,即把属于星花的给删除了a++; // 长度加1 加结束符*a=‘\0‘;//*(a+1)=‘\0‘;}
void main(){char s[100];printf("Enter the string :\n");gets(s);fun(s);printf("The string after deleted :\n");puts(s);    }

对于特殊字符串的处理方法

标签:

原文地址:http://www.cnblogs.com/Mr210843013/p/4783161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!