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

对于特殊的字符串,我们对字符串进行特殊与非特殊两种,第一种字符串开头特殊字符提到末尾处理方法或者末尾字符串提到开头,总之先处理特殊的就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,int n,int h,int e)
{
int i,j=0;
for(i=h;i<n-e;i++)
a[j++]=a[i];
a[j]='\0';
}
void main()
{ char s[81],*t,*f;
int m=0,tn=0,fn=0;
printf("Enter a string:\n ");
gets(s);
t=f=s;
while(*t)
{
t++;
m++;
}
t--; //指向最后一个字符
while(*t=='*')
{
t--;
tn++; //统计末尾字符*的个数
}
while(*f=='*')
{
f++;
fn++;
}  //统计开头字符*的个数
fun(s,m,fn,tn);
printf("The string after deleted :\n");
puts(s); 
}

 

posted @ 2015-09-05 13:59  Mr__sun  阅读(545)  评论(0编辑  收藏  举报