1.题目
2.代码
#include <stdio.h>
#include <string.h>
/*定义函数fun*/
void fun(char *s, char t[])
{
int i,j=0;
char ch=0;
for(i=0;s[i]!=0;i++)
{
ch=s[i];
if(i%2!=0&&ch%2!=0)
{
t[j++]=s[i];
}
}/*将字符串中符合条件的放到字符数组t中*/
t[j]='\0';/*给字符数组t赋上结束符*/
for(i=0;i<j;i++)
{
s[i]=t[i];
}
for(i = j; j < 100; j++)/*第21行*/
{
s[i] = 0;
}/*将字符数组t赋给数组s,并将数组s剩余部分清零。*/ /*第24行*/
}
main()
{
char s[100], t[100];void NONO ();
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
}
3.知识点:
如何利用代码求一个字符的ASII码值
可以通过将字符直接赋值给一个 int
类型变量来获取其 ASCII 码值。
代码为
#include <stdio.h>
int main()
{
char ch = 'A'; int asciiValue = ch; // 将字符赋值给 int 类型变量,获取其 ASCII 码值
printf("The ASCII value of %c is %d\n", ch, asciiValue);
return 0;
}
4.注意:
不要忘记在字符数组 t 的末尾添加字符串终止符
第21行到第24行的功能是将数组s剩余部分清零,如果不清零原始字符串的剩余部分也不会导致代码错误,但在某些情况下可能会导致意外行为。