Why can not I enter two strings with spaces?

advertisements

I've written a program that accepts a string called source. It can then accept another string called insertion. The user then chooses which position in source that they'd like to insert insertion. It then prints back the result.

The program runs perfectly well when I input a string such as "the young son". I can simply add [^\n] during the scanf function for source[].

If I then input "per" using a regular scanf and choose to place it at position = 10, then I get a nicely printed out "the young person".

However, if I choose to have an insertion with a space, such as "fat " to put into "the cat", hoping for a completed "the fat cat", then it will ignore the space if I proceed as normal and bunch it together.

OR, if I add [^\n] into the scanf for insertion too, it actually decides to cancel out the previous, and it doesn't even let me input an insertion, it just uses "the" and "cat" and assumes they're separate.

Is there any way around this?

Please keep in mind that this was a learning exercise(and I've already passed the criteria, this is just me being nitpicky) in which I am not allowed to use any library functions aside from printf and scanf. I'm also not able to use any pointer syntax as it has not been covered yet and I don't want to skip ahead.

Thank you!

Here is my code below:

#include <stdio.h>

void insertString(char source[], char insertion[], int position);
int stringLength(const char string[]);

int main(void)
{
    int position;
    char source[40];
    char insertion[40];

    printf("What's your first string?\n");
    scanf("%[^\n]s", source);

    printf("What do you want to insert?\n");
    scanf("%[^\n]s", insertion);

    printf("Where do you wanna put it?\n");
    scanf("%i", &position); // 

    // call function
    insertString(source, insertion, position);

    // print out result, stored in source
    printf("Here's the result: %s\n", source);

    return 0;
}

void insertString(char source[], char insertion[], int position)
{
    int i, j;

    // Find length of string
    int lengthBig = stringLength(source);
    int lengthSmall = stringLength(insertion);
    int lengthTotal = (lengthBig + lengthSmall) -1;

    // move up source characters after position
    for(i = lengthBig - 1; i >= position; i--)
    {
        source[i + (lengthSmall) ] = source[i];
    }

    // move in insertion characters
    for(j = lengthSmall-1; j >= 0; j--)
    {
        source[position + j] = insertion[j];
    }

    // add null character
    source[lengthTotal + 1] = '\0';
}

int stringLength(const char string[])
{
    int count =0;

    while(string[count] != '\0')
        count++;

    return count;
}


consider using fgets for input and parse integer with sscanf. The size of the source array needs to be included to avoid writing too many characters. A pair of pointers can be used to iterate through the arrays in insertString.

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

#define SIZE 256

void insertString(char source[], int size, char insertion[], int position);
int stringLength(const char string[]);

int main(void)
{
    int position;
    int result = 0;
    char source[SIZE];
    char insertion[SIZE];
    char input[SIZE];

    printf("What's your first string?\n");
    if ( ( fgets( source, sizeof ( source), stdin)) == NULL) {
        printf ( "could not get input\n");
        return 1;
    }
    source[strcspn ( source, "\n")] = '\0';//remove newline

    printf("What do you want to insert?\n");
    if ( ( fgets( insertion, sizeof ( insertion), stdin)) == NULL) {
        printf ( "could not get input\n");
        return 1;
    }
    insertion[strcspn ( insertion, "\n")] = '\0';//remove newline

    do {
        printf("Where do you want to put it?\n");
        if ( ( fgets( input, sizeof ( input), stdin)) == NULL) {
            printf ( "could not get input\n");
            return 1;
        }
        result = sscanf(input, "%d", &position);
    } while ( result != 1 || position < 0);//loop on bad input

    insertString(source, sizeof ( source), insertion, position);

    printf("Here's the result: %s\n", source);
    return 0;
}

void insertString(char source[], int size, char insertion[], int position)
{
    char *to = NULL, *from =NULL;

    // Find length of string
    int lengthBig = stringLength(source);
    int lengthSmall = stringLength(insertion);
    int lengthTotal = (lengthBig + lengthSmall);

    if ( lengthTotal >= size) {
        printf ( "not enough space\n");
        return;
    }

    if ( position > lengthBig) {
        position = lengthBig;
    }

    //shift to make room for insertion
    to = &source[lengthTotal + 1];
    from = &source[lengthBig + 1];

    while(from != &source[position])//loop back to source[position]
    {
        *to = *from;
        to--;
        from--;
    }
    *to = *from;

    //put insertion into source
    to = &source[position];
    from = insertion;

    while(*from)//loop until '\0'
    {
        *to = *from;
        to++;
        from++;
    }
}

int stringLength(const char string[])
{
    int count =0;
    while(*string) {
        count++;
        string++;
    }
    return count;
}