77!!

77 date:2021.2.25
在这里插入图片描述

要点:
字符串复制函数strcpy()

详细代码如下:

#include    <stdio.h>
#include    <string.h>
#define    M    5
#define    N    20
void fun(char  (*ss)[N])
{
    
      int  i, j, k, n[M];      char  t[N];
   for(i=0; i<M; i++)  n[i]=strlen(ss[i]);
   for(i=0; i<M-1; i++)
   {
    
      k=i;
/**********found**********/
      for(j=i+1; j<M; j++)
/**********found**********/
        if(n[k]>n[j])  k = j; //较小下标
      if(k!=i)
      {
    
      strcpy(t,ss[i]);
         strcpy(ss[i],ss[k]);
/**********found**********/
         strcpy(ss[k],t);
         n[k]=n[i];
      }
   }
}
void main()
{
    
      char  ss[M][N]={
    
    "shanghai","guangzhou","beijing","tianjing","cchongqing"};
   int  i;
   printf("\nThe original strings are :\n");
   for(i=0; i<M; i++)  printf("%s\n",ss[i]);
   printf("\n");
   fun(ss);
   printf("\nThe result :\n");
   for(i=0; i<M; i++)  printf("%s\n",ss[i]);
}


在这里插入图片描述

要点:
有点不理解

详细代码如下:

#include <stdio.h>
#include <string.h>
/**********found**********/
void fun(char *str, char ch )
{
    
       while (  *str && *str != ch )
	 str++;
/**********found**********/
    if(  *str != ch  )  //不相等
    {
    
      str [ 0 ] = ch;
/**********found**********/
       str[1] = '\0';  //字符串结束符
    }
}

void main( )
{
    
        char  s[81],  c ;
     printf( "\nPlease enter a string:\n" );  gets ( s );
     printf ("\n Please enter the character to search : " );
     c = getchar();
     fun(s, c) ;
     printf( "\nThe result  is %s\n",  s);
}


在这里插入图片描述
要点:

逆序输出

详细内容如下:

#include <string.h>
#include <conio.h>
#include <stdio.h>
#define N 81
void fun(char*s)
{
    
    
	/*
		analyse:

		逆序输出
	*/
	int i,j,m,n;
	char ch; 
	
	i = 0;
	m = n =strlen(s)-1;

	while(i < (n+1)/2) //while(i < j)  长度为n的字符串需要交换的次数为n/2
	{
    
    
		ch = s[i];
		s[i] = s[m];
		s[m] = ch;

		i++;
		m--;
	}



	/*  error: 想不通

	int i;
	int j = strlen(s);

	for(i = j-1; i >= 0; i--)
	{
		*s = s[i];
		s++;
		
	}
	s= '\0';
	*/
}

void main()
{
    
    
	 char a[N];
	 FILE *out;
	 printf("Enter a string:");
	 gets(a);
	 printf("The  original string is:");
	 puts(a);
	 fun(a);
	 printf("\n");
	 printf("The string after modified:");
	 puts(a);
	 strcpy(a,"Hello World!");
	 fun(a);
	 /******************************/
	 out=fopen("out.dat","w");
	 fprintf(out,"%s",a);
	 fclose(out);
	 /******************************/
}

猜你喜欢

转载自blog.csdn.net/weixin_44856544/article/details/114107111
77