×

连接两个字符串不使用strcat函数

连接两个字符串不使用strcat函数(编写一个程序,将两个字符串连接起来,不用strcat函数)

admin admin 发表于2024-09-21 08:38:10 浏览2 评论0

抢沙发发表评论

各位老铁们好,相信很多人对连接两个字符串不使用strcat函数都不是特别的了解,因此呢,今天就来为大家分享下关于连接两个字符串不使用strcat函数以及编写一个程序,将两个字符串连接起来,不用strcat函数的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

本文目录

编写一个程序,将两个字符串连接起来,不用strcat函数

我觉得你应该先描述一下你为什么认为会有 j=j+1......

首先我猜,可能是逻辑没理清。

首先要明白,在第二个 wile 语句执行前,i 其实是处于字符串最末尾的’\0’位置,而 j 是等于 0。

而每个字符串末尾的’\0’就是用来标识一个字符串结束的,所以拼接字符串其实就是从串 1 的末尾’\0’位置开始讲串 2 的内容全部一个一个的粘贴过去(覆盖’\0’),然后在最后的末尾在添加一个 ’\0’用以标识串的结束。

---

但是我又猜,万一是不明白自增、自减的含义呢?

s1;

i++、j++ 意思是在语句结束后,各自加一。

所以

s1;

等同于:

s1;i += 1;j += 1;

不过上面不是严格的等同,但是日常等同过去没问题,原理就涉及到汇编了。

---

最后我又想,也许楼主都会,只是因为代码十分的不友好,看走眼了呢?

所以我又放上增加了一点可读性(加了花括号)的原代码:

#include 《stdio.h》int main(){    char s1;    int i=0,j=0;    printf("input string1:");    scanf("%s",s1);    printf("input string2:");    scanf("%s",s2);    while (s1!=’\0’)    {        i++;    }    while(s2!=’\0’)    {        s1;    }    s1=’\0’;    printf("\nThe new string is:%s\n",s1);    return 0;}

最后的最后,怎么提问实在是一门精妙的艺术。

C语言:编一程序,将两个字符串连接起来 要求:不允许使用strcat函数

#include《stdio.h》#include《stdlib.h》/*程序入口点函数*/int main(){      int i,j;    char str1;    gets(str1);    gets(str2);    for(i=0;str1!=’\0’;i++)             str3;    for(j=0;str2!=’\0’;j++)        str3;        str3=’\0’;    printf("%s\n%s\n%s\n",str1,str2,str3);    system("pause");    return 0;}

不使用strcat函数,编写一个程序将两个字符串连接起来

简单main(){char a;int i,j;for(i=0;a!=’\0’;i++);i--;for(j=0;b!=’\0’;i++,j++)a;a=’\0’;printf("%s\n",a);}我没试,大概应该是没什么问题就算要改也改不了多少还是你自己试试看吧Z^_^

输入两个字符串,并将短串连接到长串后面(不用 strcat函数)

程序不是已经实现了题目的要求吗?

#include《stdio.h》#include《string.h》int main(){  int i,m=0,n=0;  char c1, c2;  char str1;  printf("Input str1:\n");  gets(str1); //从你的键盘上读取你输的字符放到字符(不是字符串数组)数组里.  printf("The str1 is:\n");  for (i = 0; (c1 = str1) != ’\0’; i++)  {    m++;//统计串长    printf("%c", str1);//输出串中的字符  }  printf("\n");//换行  printf("m=%d\n", m); //输出串长  printf("Input str2:\n");  gets(str2);  printf("The str2 is:\n");  for (i = 0; (c2 = str2) != ’\0’; i++)  {    n++;//统计串长    printf("%c", str2);//输出串中的字符  }  printf("\n");  printf("n=%d\n",n);  if (n 《= 80 - (m + 1))//如果总串长未超长  {    printf("Now we connect str1 and str2:\n");    for (i = 0; i 《 m + n; i++)    {      c1 = str1;      if(i《=n)//把str2中的字符复制到str1中,并防止越界       {        c2 = str2;        str1 = c2;      }      printf("%c", str1);    }  }  else    printf("There is an error in your input,please try again!");  printf("\n");  return 0;}

以上就是我们为大家找到的有关“连接两个字符串不使用strcat函数(编写一个程序,将两个字符串连接起来,不用strcat函数)”的所有内容了,希望可以帮助到你。如果对我们网站的其他内容感兴趣请持续关注本站。