How to write a program that connects three strings.

Asked 2 years ago, Updated 2 years ago, 32 views

As shown below, I would like to enter a program that allows me to enter three strings and print out the three strings, but my program didn't work.

Example Execution

Please enter string 1:One
Please enter string 2: Two
Please enter string 3:Three
Concatenated Results: OneTwoThree

source code

int main (void)
{
    char*s1[128], *s2[128], *s3[128];
    printf("Please enter string 1:");
    scanf("%s", s1[128]);
    printf("Please enter string 2:");
    scanf("%s", s2[128]);
    printf("Please enter string 3:");
    scanf("%s", s3[128]);

    printf("%s\n", s1[128]);
    printf("%s\n", s2[128]);
    printf("%s\n", s3[128]);


    return 0;
}

compilation results

$./a.out
Please enter string 1 : One
Please enter string 2: Please enter string 3: Two
Segmentation fault

I don't even know why the "Please enter string 2" and "Please enter string 3" parts are not identified.How can I program it so that I can compile it well?

c

2022-09-30 14:01

2 Answers

Almost every line is bad, but if you explain why it's bad in order:

The variable definition below defines an area that contains 128 pointers to characters, not a storage area for strings (128 characters).

char*s1[128], *s2[128], *s3[128];

The following parameters specify the 128th pointer (which should not exist because it starts at 0th) in the pointer array to the letter s1.

scanf("%s", s1[128]);

This is the same as above.In fact, segmentation fault has occurred without running up to this line."Also, the input values are displayed one by one without ""consolidation""."Do you mean that you couldn't get around to the consolidation process?

printf("%s\n", s1[128]);

Enter the string 1 since you have specified the address value of the pointer that should not have been present (and not initialized): It is no surprise that Segmentation fault occurs when the One is completed.
Please enter string 2: string 3: It was a coincidence that it ran until Two.

It would be better to use a new function with the following safety in mind.(Regarding the VC++ document)
scanf_s,_scanf_s_l, wscanf_s,_wscanf_s_l
strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l
strcat_s, wcscat_s, _mbscat_s, _mbscat_s_l

In fact, it would be better to detect and deal with errors, but the program is as follows:An overview is provided in the comments.

#include<stdio.h>
# include <stdlib.h>
# include <string.h>
# include <errno.h>
# define MAX_INPUT_LEN 128/* Maximum input length +1*/
#defineMAX_CONCAT_LEN384/*Maximum concatenation length (three times the value)*/

int main (void)
{
    chars1[MAX_INPUT_LEN] = {0}; /* Character array area for storing input characters: Initialize */
    chars2[MAX_INPUT_LEN] = {0}; /* Same as above */
    chars3[MAX_INPUT_LEN] = {0}; /* Same as above */
    chars4[MAX_CONCAT_LEN] = {0}; /* Character array area for concatenation results storage: Initialize */

    printf("Please enter string 1:");
    scanf_s("%127s", s1,MAX_INPUT_LEN); /* Each input action: Use scanf_s and specify a maximum length */
    printf("Please enter string 2:");
    scanf_s("%127s", s2,MAX_INPUT_LEN);
    printf("Please enter string 3:");
    scanf_s("%127s", s3,MAX_INPUT_LEN);

    strcpy_s(s4,MAX_CONCAT_LEN,s1); /* Concatenation begins with string copy*/
    strcat_s(s4,MAX_CONCAT_LEN,s2); /* 2nd and 3rd are concatenated*/
    strcat_s(s4,MAX_CONCAT_LEN,s3);
    printf("Consolidated results: %s\n", s4);

    return 0;
}


2022-09-30 14:01

This is what it looks like to fix it simply.

#include<stdio.h>

int main (void)
{
    chars1[128], s2[128], s3[128];
    printf("Please enter string 1:");
    scanf("%s",s1);
    printf("Please enter string 2:");
    scanf("%s", s2);
    printf("Please enter string 3:");
    scanf("%s",s3);

    printf("%s",s1);
    printf("%s",s2);
    printf("%s\n",s3);


    return 0;
}


2022-09-30 14:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.