• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 30, 2022 |440 Views
C Program to Trim Leading White Spaces from String
  Share   Like
Description
Discussion

In this video, will write a C Program to Trim Leading White Spaces from String.

Strings are defined as an array of characters. A string is that the string is terminated with a special character ‘\0’ and whitespace is a term that refers to characters that are used for formatting purposes.

Examples :
Input: str = ” geeksforgeeks”
Output: geeksforgeeks

Method:
The idea is to count the leading spaces in the given string and then from that count index copy the characters from that index to the front of the string.

Algorithm:

Step 1: Initialize count = 0 to count the number of leading spaces.
Step 2: Iterate through the given string and find the index (say idx) at which the leading space character ends.
Step 3: Iterate through all the characters from that index idx and copy each character from this index to the end to the front index.
Step 4: Finally, put ‘\0’ at the last index of the new string to terminate the string.
Step 5: Print output.

So the Time and Space complexity of this method is as follows:
Time Complexity: O(N)
Auxiliary Space: O(N)

C Program to trim leading white spaces from string: 
https://www.geeksforgeeks.org/c-program-to-trim-leading-white-spaces-from-string/

Read More