2

I'm learning string in c, and i'm working on my homework which ask me to write a program to replace part of string under certain circumstances. Here is my source code(undone):

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[128], str2[128], str3[128];
    for (int i = 0; i < 128; i++) //initialize str
    {
        str1[i] = 0;
        str2[i] = 0;
        str3[i] = 0;
    }
    printf("Input the first string:"); //inputs
    fgets(str1, 128, stdin);
    printf("Input the second string:");
    fgets(str2, 128, stdin);
    printf("Input the third string:");
    fgets(str3, 128, stdin);
    if (strncmp(str1, str2, strlen(str2) - 1) == 0) //if the first n charters match (n=length of str2)
    {
        printf("%s", str3); //print str3
        int RemainingChar = 0;
        RemainingChar = strlen(str1) - strlen(str2);
        for (int i = 0; i < RemainingChar; i++)
        {
            printf("%c", str1[i + strlen(str2) - 1]); //print the remaining part
        }
    }
    return 0;
}

Here is how it run:

Input the first string:asdfghjkl
Input the second string:asd
Input the third string:qwe
qwe
fghjkl

There is an unexpected line break. what should I do to make it output like this:qwefghjkl?

  • 1
    Welcome to SO. That's a bit puzzling. You seem to be aware of the `\n` stored in buffer. Otherwise this would not go together: `strlen(str2)-1` and comment `(n=length of str2)`. But when printing, you forgot about it... – Gerhardh Nov 16 '21 at 14:34
  • You can initialize your arrays much easier: `char str1[128] = "";`. This will fill everything with 0 bytes. – Gerhardh Nov 16 '21 at 14:35
  • @Gerhardh And the question must be asked just *why* one would feel compelled to zero the strings. `fgets()` zero-terminates the input. – Peter - Reinstate Monica Nov 16 '21 at 14:36
  • @Gerhardh Thanks for teaching me a new way to initialize a string. :) Actually I haven't learned buffer, I only have a general feeling about it. :( – WallBreakerNO4 Nov 16 '21 at 14:39
  • Actually that is not directly related to strings. For all structs or arrays you can provide less initializer values than elements. In that case all remaining elements will be set to 0. In my example `""` is just an empty string, i.e. only one single 0 byte and all the other bytes will automatically be set to 0. – Gerhardh Nov 16 '21 at 14:45
  • @Gerhardh Or, more generally: `char str1[128] = {'\0'};`, which initializes the first character explicitly and the remaining ones implicitly with zero bytes. Unfortunately, the C syntax does not permit to (zero-)initialize an array (or any compound type) with empty braces: The legal C++ definition `char str1[128] = {};` is not legal C; see the discussion [here](https://stackoverflow.com/questions/17589533/is-an-empty-initializer-list-valid-c-code). Many compilers simply accept it though, because why not. [Ah, I see that your newest comment also goes in this direction.] – Peter - Reinstate Monica Nov 16 '21 at 14:46

1 Answers1

7

The function fgets will also store the newline character '\n' at the end of the line into the string. If you don't want this newline character to be printed, then you must remove it before printing the string.

See the following question for several ways to remove the newline character:

Removing trailing newline character from fgets() input

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39