⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex5_4.cpp

📁 谭浩强 c程序学习时的程序 希望对初学者有借鉴意义
💻 CPP
字号:
#include <stdio.h>
#include <string.h>

void strcat(char *s,char *t)
{
  s=s+strlen(s);
  while(*s++=*t++)
    ;
}

void strncpy(char *s, char *t,int n)
{
  int i=1;
  while (i<=n && (*s++ = *t++) != '\0')
    i++;
  if(*(s-1)!='\0')
    *s='\0';
}

void strncat(char *s,char *t,int n)
{
  int i=1;
  s=s+strlen(s);
  while(i<=n && (*s++=*t++)!='\0')
    i++;
  if(*(s-1)!='\0')
    *s='\0';
}

int strncmp(char *s, char *t,int n)
{
  int i;
  for (i = 1; *s == *t && i<=n; s++,t++,i++)
    if (*s == '\0' || i==n)
      return 0;
  return *s - *t;
}

void main()
{
  char s[100],t[100],s_temp[100];
  int res,n;

  printf("please input string s:");
  gets(s);
  printf("please input string t:");
  gets(t);
  printf("please input n:");
  scanf("%d",&n);

  strcpy(s_temp,s);
  strcat(s,t);
  printf("strcat result is %s\n",s);
  strcpy(s,s_temp);
  strncpy(s,t,n);
  printf("strncpy result is %s\n",s);
  strcpy(s,s_temp);
  strncat(s,t,n);
  printf("strncat result is %s\n",s);
  strcpy(s,s_temp);
  res=strncmp(s,t,n);
  printf("strncmp result is %d\n",res);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -