45 (delete more than n leading * signs and output)

45 date: 2021.3.17
Insert picture description hereKey points:

The detailed code is as follows:

#include  <stdio.h>
#include  <string.h>
#define   N   5
#define   M   10
/**********found**********/
void fun(char  (*ss)[M], int  k)  
{
    
     int  i=0  ;
/**********found**********/
  while(i< N) {
    
      //遍历N个字符串
/**********found**********/
    ss[i][k]='\0';  i++;  } //用 '\0'来锁定,即加入字符串结束标识'\0',就完成了终止
}
void main()
{
    
     char  x[N][M]={
    
    "Create","Modify","Sort","skip","Delete"};
  int  i;
  printf("\nThe original string\n\n");
  for(i=0;i<N;i++)puts(x[i]);  printf("\n");
  fun(x,4);
  printf("\nThe string after deleted :\n\n");
  for(i=0; i<N; i++)  puts(x[i]);  printf("\n");
}


Insert picture description here
Key points:

The detailed code is as follows:

#include <stdio.h>
void  fun( char *a, int  n )
{
    
    
  /*
	analyse:

	目标:使字符串的前导*不多余n个,若多于n个,则删除多于的*号
	 
    遍历求字符串前导星号个数,从多于处开始重接
 */


	int i = 0;
	int k = 0;
	char *p , *t;

	p = t =a;  // p与 t同时指向数组的首地址
	while(*t == '*')
	{
    
    
		k++;
		t++;
	}
	if(k > n)
	{
    
    
		while(*p)
		{
    
    
			a[i] = *(p+k-n);
			i++;
			p++;
		}
		a[i] = '\0';
	}

	/* ERROR:
	int i,j = 0;

	for(i = 0; a[i] == '*'; i++);
	
		
	if(i >  n)
		for(j= i-n;  a[j] != '\0'; j++)
			a[i] = a[j];
		a[i] = '\0';
	*/


}

void main()
{
    
      char  s[81];  int  n;void NONO ();
   printf("Enter a string:\n");gets(s);
   printf("Enter n :  ");scanf("%d",&n);
   fun( s,n );
   printf("The string after deleted:\n");puts(s);
   NONO();
}

Guess you like

Origin blog.csdn.net/weixin_44856544/article/details/114985602