C语言:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,-将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,-将形参s所指字符串中的所有数字字符顺序前移,

时间:2023-03-10 07:25:21
C语言:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,-将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,-将形参s所指字符串中的所有数字字符顺序前移,

//函数fun功能:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,s所指串中剩余的字符形成的新串放在t所指的数组中。

 #include <stdio.h>
#include <string.h> void fun(char *s, char t[])
{
int i=,j=;
while (s[i] != '\0')
{
if (i % == )
{
if ((int)(s[i]) % == )//判断ASCII值,使用(int)强制转换类型。
{
//printf("%d", (int)s[i]);//调试语句
i++;
}
else
{
t[j] = s[i];
//printf("%c", *t);
i++; j++;
}
}
else
{
t[j] = s[i];
//printf("%c", *t);
i++; j++;
}
}
t[j] = '\0';//切记,切记,在赋值一个新数组的时候要记得添加结束符。
} void main()
{
char s[], t[];void NONO ();
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
NONO();
} void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
char s[], t[] ;
FILE *rf, *wf ;
int i ; rf = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = ; i < ; i++) {
fscanf(rf, "%s", s) ;
fun(s, t) ;
fprintf(wf, "%s\n", t) ;
}
fclose(rf) ;
fclose(wf) ;
}

//函数fun功能:首先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,按排序合并到c所指的数组,过长的剩余字符连接在数组的尾部。

 #include <stdio.h>
#include <string.h> void fun( char *a, char *b, char *c )
{
int i , j; char ch;
i = ; j = strlen(b)-;//指向尾部
/************found************/
while ( i < j )//首尾对应交换顺序。
{ ch = b[i]; b[i] = b[j]; b[j] = ch;
i++; j--;
}
while ( *a || *b ) {//一个一个进行放置
/************found************/
if ( *a )
{ *c = *a; c++; a++; }
if ( *b )
{ *c = *b; c++; b++; }
}
*c = ;//结束符
} void main()
{
char s1[],s2[],t[];
printf("\nEnter s1 string : ");scanf("%s",s1);
printf("\nEnter s2 string : ");scanf("%s",s2);
fun( s1, s2, t );
printf("\nThe result is : %s\n", t );
}

//函数fun功能:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数的返回值。

 #include  <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *fun(char *s)
{ int i, j, k, n; char *p, *t;
n=strlen(s)+;//求出长度
t=(char*)malloc(n*sizeof(char));//申请内存
p=(char*)malloc(n*sizeof(char));
j=; k=;
for(i=; i<n; i++)
{ if(isdigit(s[i])) {//函数判断是否为数字字符。
/**********found**********/
p[j]=s[i]; j++;}
else
{ t[k]=s[i]; k++; }
}
/**********found**********/
for(i=; i<k; i++) p[j+i]= t[i];//添加在p数组后面
p[j+k]=;//结束符
/**********found**********/
return p;
}
void main()
{ char s[];
printf("Please input: "); scanf("%s",s);
printf("\nThe result is: %s\n",fun(s));
}

//第一题标准答案。

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