c语言编程题

1、把字符串中ASCⅡ为奇数,元素下标为偶的删除

#include<stdio.h>
#include<string.h>
void fun(char*s,char t[])
{

int i,j=0,n;
n=strlen(s);
for(i=0;i<n;i++)
if(i%2!=0&&s[i]%2!=0)
{t[j]=t[i];j++;}
t[j]='';

}
main()
{
char s[100],t[100];
printf("
Please enter string s:");
scanf("%s",s);
fun(s,t);
printf("
The result is:%s
",t);
}

2、删除空格

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void fun (char *str)
{
int i=0;
char *p=str;
while(*p)
{if(*p!=''){str[i]=*p;i++;}
p++;
}
str[i]='';
}
main()
{
char str[81];
char Msg[]="Input a stirng";
int n;
printf(Msg);
gets(str);
puts(str);
fun(str);
printf("***str:%s
",str);
}


3、删除结尾*

#include<stdio.h>
#include<conio.h>
void fun(char *a)
{
while(*a!='')
a++;
a--;
while(*a=='*')
a--;
*(a+1)='';
}
void main()
{
char s[81];
printf("Enter a string:
");
gets(s);
fun(s);
printf("The string after deleted:
");
puts(s);
}


4、判断结尾*是否大于n,大于则删除

#include<stdio.h>
void fun(char *a,int n)
{
int i=0,k=0;
char *p,*t;
p=t=a;
while(*t)
t++;
t--;
while(*t=='*')
{k++;t--}
if(k>n)
{while(*p&&p<t+n+1){
a[i]=*p;
i++;p++;
}
a[i]='';}
}


5、保留两端*,删除字母间*

#include<stdio.h>
void fun(char *a,char *h,char *p)
{
int i=0;
char *q=a;
while(q<h)
{a[i]=*q;q++;i++;}
while(q<p)
{if(*q!='*'){a[i]=*q;i++;}q++;}
while(*q)
{a[i]=*q;i++;q++;}
a[i]='';
}
main()
{
char s[81],*t,*f;
printf("Enter a string:
");
gets(s);
t=f=s;
while(*t)
t++;
t--;
while(*t=='*')
t--;
while(*f=='*')
f++;fun(s,f,t);
printf("The string after deleted:
");
puts(s);
}


6、只保留尾部*

#include<stdio.h>
void fun(char *a,char *p)
{
char *t=a;
for(;t<p;t++)
if(*t!='*')
*(a++)=*t;
for(;*t!='';t++)
*(a++)=*t;
*a='';
}
void main()
{
char s[81],*t;
printf("Enter a string:
");
gets(s);
t=s;
while(*t)
t++;
t--;
while(*t=='*')
t--;
fun(s,t);
printf("The string after deleted:
");
puts(s);
}


7、删除重复数组

int fun(int a[],int n)
{
int i,j=1;
for(i=0;i<n;i++)
if(a[j-1]!=a[i])
a[j++]=a[i];
return j;
}


8、降序排列学生分数

void fun(STREC a[])
{
int i,j;
STREC t;
for(i=0;i<Nli++)
for(j=0;j<N;j++)
if(a[i].s<a[j+1].s)
{t=a[j];a[j]=a[j+1];a[j]=t;}
}


9、寻找回文

int fun(char *str)
{
int i,n=0,fg=1;
char *p=str;
while (*p){n++;p++;}
for(str[i]==str[n-1-i]);
else{fg=0;break;}
return fg;
}


10、字符串内容倒置

void fun(char*s)
{
char ch;
int i,m,n;
i=0;
m=n=strlen(s)-1;
while(i<(n+1)/2)
{
ch=s[i];
s[j]=s[m];
s[m]=ch;
i++;m--;
}
}
原文地址:https://www.cnblogs.com/danznb/p/3565164.html