Help in C language please!!

1. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

Comments

  • Hi.
    First of all, i would like you to know that if you are receiving strings in c console applications, it doesn't receive blank spaces (i use dev c++). But what you wish to do is possible. The steps you will need are;

    a. Receive string as input.
    b. Convert the string to a character array.
    c. Using a loop test if each array element is a space.
    d. Use a variable to indicate when the array element is a space. (a Boolean variable best suites this. It should be set to false if a space is not found and set to true if a space is found)
    e. If the Boolean variable is true, then don't modify your output.
    f. If the Boolean variable is false, then modify your output.

    I wrote the code below to show you what i mean;


    #include
    #include
    #include



    int main()
    {
    int myint = 0; //integer variable used to indicate the presence of a space
    int i = 0; //integer variable used to move through the array
    int count = 0; // integer variable used for updating the output array
    char mystring[12]; //Input string
    char myoutput[12]; //Output String

    //Setting out input string to: qwe t t y
    //Notice it has 3 consecutive spaces, then one space then two consecutive spaces
    mystring[0] = 'q';
    mystring[1] = 'w';
    mystring[2] = 'e';
    mystring[3] = ' ';
    mystring[4] = ' ';
    mystring[5] = ' ';
    mystring[6] = 't';
    mystring[7] = ' ';
    mystring[8] = 't';
    mystring[9] = 't';
    mystring[10] = ' ';
    mystring[11] = 't';

    for ( i = 0; i <= 11; i++)
    {
    if ( mystring[i] == ' ' ) // If the input string finds a blank space
    {
    if (myint == 0) //If our indicator variable if 0 meaning previous was not a blank space
    {
    myint = 1; //Change indicator variable to 1 meaning a blank space found
    myoutput[count] = mystring[i]; //Update output
    count ++ ; // increase count to move to next space in output array
    }
    }

    else if (mystring[i] != ' ') // if our input string doesn't find a blank space
    {
    myint = 0; //Change indicator variable to 0 meaning a blank space not found
    myoutput[count] = mystring[i]; //Update output
    count ++; // increase count to move to next space in output array
    }
    }

    // Printing of input and output to display the difference
    printf("Old String: ");
    printf("%s", mystring);
    printf("
    ");
    printf("New String: ");
    printf("%s", myoutput);
    printf("
    ");
    system("pause");
    }


  • Hi.
    First of all, i would like you to know that if you are receiving strings in c console applications, it doesn't receive blank spaces (i use dev c++). But what you wish to do is possible. The steps you will need are;

    a. Receive string as input.
    b. Convert the string to a character array.
    c. Using a loop test if each array element is a space.
    d. Use a variable to indicate when the array element is a space. (a Boolean variable best suites this. It should be set to false if a space is not found and set to true if a space is found)
    e. If the Boolean variable is true, then don't modify your output.
    f. If the Boolean variable is false, then modify your output.

    I wrote the code below to show you what i mean;


    #include
    #include
    #include



    int main()
    {
    int myint = 0; //integer variable used to indicate the presence of a space
    int i = 0; //integer variable used to move through the array
    int count = 0; // integer variable used for updating the output array
    char mystring[12]; //Input string
    char myoutput[12]; //Output String

    //Setting out input string to: qwe t t y
    //Notice it has 3 consecutive spaces, then one space then two consecutive spaces
    mystring[0] = 'q';
    mystring[1] = 'w';
    mystring[2] = 'e';
    mystring[3] = ' ';
    mystring[4] = ' ';
    mystring[5] = ' ';
    mystring[6] = 't';
    mystring[7] = ' ';
    mystring[8] = 't';
    mystring[9] = 't';
    mystring[10] = ' ';
    mystring[11] = 't';

    for ( i = 0; i <= 11; i++)
    {
    if ( mystring[i] == ' ' ) // If the input string finds a blank space
    {
    if (myint == 0) //If our indicator variable if 0 meaning previous was not a blank space
    {
    myint = 1; //Change indicator variable to 1 meaning a blank space found
    myoutput[count] = mystring[i]; //Update output
    count ++ ; // increase count to move to next space in output array
    }
    }

    else if (mystring[i] != ' ') // if our input string doesn't find a blank space
    {
    myint = 0; //Change indicator variable to 0 meaning a blank space not found
    myoutput[count] = mystring[i]; //Update output
    count ++; // increase count to move to next space in output array
    }
    }

    // Printing of input and output to display the difference
    printf("Old String: ");
    printf("%s", mystring);
    printf("
    ");
    printf("New String: ");
    printf("%s", myoutput);
    printf("
    ");
    system("pause");
    }


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion