If I incompletely populate my array of characters then what would be the values stored in the addresses which are empty?

My task is to append a "." (dot) after the last element of an array of characters and my strategy is to add the dot as soon as I encounter a '\0' in my search.But even if the array has elements lesser than the space allocated to it, the dot is appended at the last location. For example if I have allocated an array of size 10 and I input only 5 characters, then also the dot is appended to the array in the 10th place.How can I get around with this ? Following is the C code that I have written :

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i;
    char dot = '.';
    char string_input[10];
    fgets (string_input, sizeof(string_input), stdin);
    int s = sizeof(string_input);
    for(i=0;i<=s;++i)
    {
        if (string_input[i]!= '\0')
    {
        continue;
        }
            else
            {
                string_input[i]=dot;
                printf("Hello, %s\n", string_input);
        break;
            }
    }

    return 0;
}

Answers


To append dots up to the end of the character array try the following

#include <stdio.h>

int main(void) 
{
    char dot = '.';
    char string_input[10];
    const size_t N = sizeof( string_input );

    fgets( string_input, N, stdin );

    size_t i = 0;
    while ( i < N - 1 && string_input[i] != '\n' && string_input[i] != '\0' ) ++i;

    while ( i < N - 1 ) string_input[i++] = dot;
    string_input[i] = '\0';

    printf( "\"%s\"\n", string_input );

    return 0;
}

Take into account that function fgets also reads the new line character that corresponds to the pressed key Enter provided that it has enough space to store it.

If to enter for example

abcd

then output will be

"abcd....."

If you want to add only one dot to the end of the string then you can write

#include <stdio.h>

int main(void) 
{
    char dot = '.';
    char string_input[10];
    const size_t N = sizeof( string_input );

    fgets( string_input, N, stdin );

    size_t i = 0;
    while ( i < N - 1 && string_input[i] != '\n' && string_input[i] != '\0' ) ++i;

    if ( i < N - 1 ) string_input[i++] = dot;
    string_input[i] = '\0';

    printf( "\"%s\"\n", string_input );

    return 0;
}

Here the output will be

"abcd."

This is because you are using

int s = sizeof(string_input);

which gives the amount on memory the string takes - in this case 10 bytes, regardless of its content.

Note also that this

for(i=0;i<=s;++i)

is incorrect since you must not attempt to index string_input[10], that index is out of range.

I suggest you use strlen() which will give the string length actually used, including the newline character appended by fgets(). Then overwrite the newline like this

for(i=0;i<s;++i)
{
    if (string_input[i] == '\n')
        {
            string_input[i]=dot;
            printf("Hello, %s\n", string_input);
            break;
        }
}

There are several fundamental issues with your code, apart from the off-by-one error pointed out by melpomene. First, if you want to append some character after a string in-place, then you should make room for it ahead of time. So instead of

fgets (string_input, sizeof(string_input), stdin);

You should write

fgets (string_input, sizeof(string_input)-1, stdin);

Then you also need to terminate your string with '\0' after overwriting it with dot. So instead of saying

string_input[i]=dot;

You should say

string_input[i]=dot;
string_input[i+1]='\0';

In terms of coding style, you need to pay attention to

  • correct indentation
  • avoiding some subtle but silly things, like if ( A!=B ) { continue; } else {...} which is simply if (A==B) {...}; also you can easily avoid invoking sizeof twice by swapping the two statements.

I'm pointing those out as I see you're probably learning the language.


Need Your Help

JPQL statement returning boolean value

select jpa jpa-2.0 jpql

Is it possible to write JPQL query like following:

Pandas Timedelta in Days

python numpy timestamp pandas

I have a dataframe in pandas called 'munged_data' with two columns 'entry_date' and 'dob' which i have converted to Timestamps using pd.to_timestamp.I am trying to figure out how to calculate ages of