How to use C program to write the following code Write a fun

How to use C program to write the following code

Write a function whose sole argument is a C string. The function removes all leading and trailing whitespace from the string. For example, if before your function is called, the string is \" What a long exam \", after the function is called, the string is \"What a long exam\". (Note: you\'re modifying the original string. You\'re not creating and returning a new string.) You may use any function in , but do not use any functions in .

Solution

// C code to trim leading and trailing whitespace
#include <stdio.h>
#include <string.h>

// function to remove leading and trialing space
void remove_Space(char * inputStr)
{
    int index;
    /// remove leading whtespace
    int spaceIndex = 0;

    // find lst index of space
    while(inputStr[spaceIndex] == \' \' || inputStr[spaceIndex] == \'\\t\' || inputStr[spaceIndex] == \'\ \')
    {
        spaceIndex++;
    }

    int index1 = 0;
    while(inputStr[index1 + spaceIndex] != \'\\0\')
    {
        inputStr[index1] = inputStr[index1 + spaceIndex];
        index1++;
    }
    // null terminate string
    inputStr[index1] = \'\\0\';


    // remove leading white space
    int index2 = 0;
    while(inputStr[index2] != \'\\0\')
    {
        if(inputStr[index2] != \' \' && inputStr[index2] != \'\\t\' && inputStr[index2] != \'\ \')
        {
            index = index2;
        }

        index2++;
    }

    inputStr[index + 1] = \'\\0\';
}

int main()
{
    char inputStr[100];

    printf(\"Enter inputStr: \");
    gets(inputStr);
   
    printf(\"Input string: \\\"%s\\\"\ \", inputStr);
    // calling fuunction
    remove_Space(inputStr);

    printf(\"Result: \\\"%s\\\"\ \", inputStr);

    return 0;
}




/*
output:

Enter inputStr:        my name is ayush verma .   
Input string: \"       my name is ayush verma .     \"
Result: \"my name is ayush verma .\"

*/

How to use C program to write the following code Write a function whose sole argument is a C string. The function removes all leading and trailing whitespace fr
How to use C program to write the following code Write a function whose sole argument is a C string. The function removes all leading and trailing whitespace fr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site