41(保留字符串中ASCII值为偶数的数)

41 date:2021.3.22
在这里插入图片描述

要点:
字符串中所有非字母元素放到字母元素后面,取值范围为0–k
可作大题思考

详细代码如下:

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
char *fun(char  *s)
{
    
     int  i, j, k, n;    char  *p, *t;
  n=strlen(s)+1;
  t=(char*)malloc(n*sizeof(char));
  p=(char*)malloc(n*sizeof(char));
  j=0; k=0;
  for(i=0; i<n; i++)
  {
    
      if(((s[i]>='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))) {
    
    
/**********found**********/
       t[j]=s[i]; j++;}
     else
     {
    
      p[k]=s[i]; k++; }
  }
/**********found**********/
  for(i=0; i<k; i++)  t[j+i]=p[i]; //字符串中所有非字母元素放到字母元素后面,取值范围为0--k
/**********found**********/
  t[j+k]= '\0';
  return  t;
}
void main()
{
    
     char  s[80];
  printf("Please input: ");  scanf("%s",s);
  printf("\nThe result is: %s\n",fun(s));
}


在这里插入图片描述
要点:
ASCII值为奇数/偶数的判定方法

详细代码如下:

#include <stdio.h>
#include <string.h>

void  fun(char  *s, char  t[])
{
    
    
	/*
		analyse:
		遍历字符串数组;
		判断ASCII值是否是奇数
		存取非奇数值到t数组中
	
	*/

	int i,j=0;
	// int n =strlen(s);   可求字符长度

	for(i = 0; s[i] != '\0'; i++)
	{
    
    
		if( s[i]%2 != 1)
			t[j++] = s[i];
	}
	t[j] = '\0';


}

void 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);
  NONO();
}

猜你喜欢

转载自blog.csdn.net/weixin_44856544/article/details/115062741