String Trim Functions In C (Remove Leading And Trailing Space)

Standard string library of c lacks trim(), ltrim() and rtrim() functions. If you have some basic knowledge of MySQL or PHP,surely you gonna miss them in C. But if know how to play with character arrays (i.e. strings) you can incarnate them in C. This C tutorial will explain you string trim functions in C.

What are string trim functions?

String trim functions are used to remove extra white-space from strings. These functions can remove white-space in string either from left side or from right side or both side.

Logic For String Trim Functions In C

Well trimming logic in C is not much difficult. We are going to follow these steps:-

  • Get input in main() using fgets function. After that, remove extra next line character (\n) using string library.
  • Create rtrim() and ltrim() which eliminate extra white-spaces and tabs (\t).
  • Call rtrim() and ltrim() functions and get desired results. Logic for rtrim() and ltrim() is mentioned below.

#1 Rtrim() logic in C

Rtrim function removes all leading spaces from input string (i.e. remove white-space from string on right side).

For rtrim function, we start reading characters of string from right side. If character is blank space then replace it by terminating zero (‘\0’). Keep repeating this till find a non blank space character.

In this way all spaces get removed by ‘\0’ and left most ‘\0’ becomes last character of string array. Thus all trailing spaces get eliminated.

#2 Ltrim() logic in C

Ltrim function removes all trailing spaces from input string str2 (i.e. remove white-space from string on left side).

For ltrim function, start scanning characters from left side. Keep skipping till position of first non space character. Store trimmed part of input string in new string ltrim. Thus all leading space removed and manipulated string stored in ltrim.

#3 Trim() logic in c

Trim function removes all leading and trailing spaces. We can combine logic of ltrim() and rtrim() inside trim function.

Remove leading and trailing blank space in C using while loop.

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

void rtrim();
void ltrim();
void trim();

void main() {

char str[30];

printf(“Enter string:-“);

// Get input from user.
fgets(str,30,stdin);

// Remove next line character ‘\n’
str[strlen(str)-1]=’\0′;

// Calling functions

rtrim(str);
ltrim(str);
trim(str);

}

// Rtrim operation starts here.

void rtrim(char a[30]) {

char rtrim[30]=”\0”;

int i=strlen(a)-1;

while (i>=0) {

// We are not considering next line character ‘\n’ because it is already removed.

if (a[i]==’ ‘|| a[i]==’\t’) {rtrim[i]=’\0′; i–;}
if (a[i]!=’ ‘ && a[i]!=’\t’) {break;}
}

while (i>=0) { rtrim[i]=a[i]; i–; }

printf(“After rtrim string is [%s] \n”,rtrim);

}

// Ltrim operation starts here.

void ltrim(char b[30]) {

char ltrim[30]=”\0″;
int i=0,j=0;

while (b[i]!=’\0′) {

if (b[i]!=’ ‘ && b[i]!=’\t’) {break;}
i++;
}

while (b[i]!=’\0′) {
ltrim[j]=b[i];
i++;
j++; }

printf(“After ltrim string is [%s] \n”,ltrim);

}

// Trim operation starts here.

void trim(char c[30]) {

char rtrim[30]=”\0″;

int i=strlen(c)-1;

while (i>=0) {

if (c[i]==’ ‘|| c[i]==’\t’) {rtrim[i]=’\0′;}
if (c[i]!=’ ‘ && c[i]!=’\t’) {break;}
i–; }

while (i>=0) {

rtrim[i]=c[i];
i–; }

char ltrim[30]=”\0″;
int j=0,k=0;
while (rtrim[j]!=’\0′)

{
if (rtrim[j]!=’ ‘ && rtrim[j]!=’\t’) {break;}
j++;
}

while (rtrim[j]!=’\0′) {
ltrim[k]=rtrim[j];
j++;
k++; }

char trim[30];
strcpy (trim, ltrim);

printf(“After trim string is [%s] \n”,trim);

}

Minimize Size Of Trim()

In above program we successfully removed extra blank space characters from both left and right. We can improve above logic by declaring functions as character pointer instead of void.

It will help to reduce size of trim function.

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

char* rtrim();
char* ltrim();
char* trim();

int main() {

char str1[30]=”First “;
char str2[30]=” Second”;
char str3[30]=” Third “;

printf(“[%s]\n”,rtrim(str1));
printf(“[%s]\n”,ltrim(str2));
printf(“[%s]\n”,trim(str3));
return 0;
}

// Rtrim () starts from here.

char* rtrim(char a[30])

{

int i=strlen(a)-1;

// We are not considering next line character ‘\n’ because it is already removed.

for (i; a[i]==’ ‘|| a[i]==’\t’; i–) {

a[i]=’\0′; }

return a; }

//Ltrim() starts from here.

char* ltrim(char b[30]) {

int i=0, j=0;
char ltrim[30]=”\0″;

for(i; b[i]==’ ‘ || b[i]==’\t’; i++);

while (b[i]!=’\0′)

{
ltrim[j]=b[i];
i++;
j++; }

strcpy(b,ltrim);

return b;

}

// Trim() starts from here.

char* trim(char c[30])

{

rtrim(c);
ltrim(c);
return c;

}

In this way you can create simple string trim functions in C. Feel free to ask your doubts in comments.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.