Question

In C programming. write a function that accepts a string (a pointer character) and deletes all...

In C programming. write a function that accepts a string (a pointer character) and deletes all the leading spaces.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

#include <stdio.h>
char* RemoveLeadingSpaces(char* a);
int main()
{
        char str1[80], *ptr;
        printf("Enter a string upto 80 characters...\n");
        fgets(str1, 80, stdin); //gets(str1);

        ptr = RemoveLeadingSpaces(str1);
        printf("String after removing leading spaces is:%s", ptr);
        return 0;
}

char* RemoveLeadingSpaces(char* a)
{
        int i;
        while(1)
        {
                if(a[0] == ' ') //Comparing first character with space if it's equal copy remove first character in array.
                {
                        for(i = 0; a[i] != '\0'; i++)
                        {
                                a[i] = a[i+1];
                        }
                }
                else   //If first character is not space then exit from the loop
                {
                        break;
                }
        }
        return a;
}

In function i am just returning the string. If you want print in function then directly in a in function.

If you have any queries you can drop a comment....

Thanks...

Add a comment
Know the answer?
Add Answer to:
In C programming. write a function that accepts a string (a pointer character) and deletes all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT