current position:Home>Input two strings a and B from the keyboard. It is required to connect the first five characters of string B to string a without using the library function strcat: if the length of B is less than 5, all elements of B will be connected to a.

Input two strings a and B from the keyboard. It is required to connect the first five characters of string B to string a without using the library function strcat: if the length of B is less than 5, all elements of B will be connected to a.

2022-01-27 01:58:18 CSDN Q & A

Enter two strings from the keyboard a and b, Library functions are not required strcat A string b The first five characters of hyphen
Receive string a in : If b The length of is less than 5, Then put b All elements of are connected to a in .




Refer to the answer 1:
int main() {    char a[20] = { 0 }, b[20];    std::cin >> a >> b;    int len = 5 < strlen(b)  ? 5 : strlen(b) ;    for (int i = 0; i < sizeof(a); i++) {        if (a[i] == '\0') {            for (int j = 0; j < len; j++) {                *(a +i+ j) = b[j];            }            *(a + i + len) = '\0';            break;        }    }    std::cout << a;    return 0;}



Refer to the answer 2:



Refer to the answer 3:
#include <stdio.h>int main(void) {     char a[1000];    char b[1000];    char c[1010];    scanf("%s",a);    scanf("%s",b);    int j=0;    for(int i=0;b[i]!='\0'&&i<5;i++){        c[j++]=b[i];    }    for(int i=0;a[i]!='\0';i++){        c[j++]=a[i];    }    c[j]='\0';    printf("%s",c);    return 0;}



Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270158162936.html

Random recommended