ch3-trim (Remove beginning and trailing whitespace)

Chapter_3     Exercise_3-6 goto     Chapter_4







trim.c     K&R, p. 65         download


#include <stdio.h> // for printf()
#include <string.h> // for strlen()

#define SIZE 100 // array size

void escape(char s[], char t[]); // copy t[] to s[] making escapes visible
int trim(char s[], int len); // remove beginning and trailing whitespace

int main()
{
char empty[] = " \t\r\n \t\v\f\n";
int len = strlen(empty);
char array[SIZE*2];
escape(array, empty); // empty does not change
printf("trim(\"%s\"[%d]): ", array, len);
len = trim(empty, len); // empty is trimmed
printf("\"%s\"[%d]\n", empty, len);

char nonempty[] = "\t \f \v\r\nHello \n\v\r\f \t";
len = strlen(nonempty);
escape(array, nonempty); // nonempty does not change
printf("trim(\"%s\"[%d]): ", array, len);
len = trim(nonempty, len); // nonempty is trimmed
printf("\"%s\"[%d]\n", nonempty, len);

char nowhite[] = "Hello";
len = strlen(nowhite);
escape(array, nowhite); // nowhite does not change
printf("trim(\"%s\"[%d]): ", array, len);
len = trim(nowhite, len); // nowhite is trimmed
printf("\"%s\"[%d]\n", nowhite, len);

return 0;
}

void escape(char s[], char t[]) // copy t[] to s[] making escapes visible
{
int i, j = 0;

for (i = 0; t[i] != '\0'; i++)
{
switch(t[i])
{
case '\'' :
s[j++] = '\\'; s[j++] = '\'';
break;
case '\"' :
s[j++] = '\\'; s[j++] = '\"';
break;
case '\t' :
s[j++] = '\\'; s[j++] = 't';
break;
case '\n' :
s[j++] = '\\'; s[j++] = 'n';
break;
case '\r' :
s[j++] = '\\'; s[j++] = 'r';
break;
case '\f' :
s[j++] = '\\'; s[j++] = 'f';
break;
case '\v' :
s[j++] = '\\'; s[j++] = 'v';
break;
case '\a' :
s[j++] = '\\'; s[j++] = 'a';
break;
case '\b' :
s[j++] = '\\'; s[j++] = 'b';
break;
case '\\' :
s[j++] = '\\'; s[j++] = '\\';
break;
default:
s[j++] = t[i]; // copy non-escapes
break;
}
}
s[j] = '\0'; // properly end string
}

// remove beginning and trailing whitespace
int trim(char s[], int len)
{
int n; // new length
for (n = len-1; n >= 0; n--)
{
if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n' &&
s[n] != '\f' && s[n] != '\v' && s[n] != '\r')
{break;} // out of for()
// else {continue;}
}
n++; // new length after trimming trailing whitespace
s[n] = '\0';
if (n == 0) {return 0;} // return n; // empty string

int i; // here s[n-1] is not whitespace, but s[] may have beginning whitespace
for (i = 0; i < n; i++)
{
if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' ||
s[i] == '\f' || s[i] == '\v' || s[i] == '\r')
{continue;}
else {break;} // out of for()
} // i counts no of beginning whitespaces
n -= i; // n = n-i; // final length
if (i == 0) {return n;} // no beginning whitespace

int j; // here i > 0
for (j = 0; j < n; j++)
{ // shift string to the beginning, thus removing
s[j] = s[j+i]; // the beginning whitespace
}
s[j] = '\0'; // s[n] = '\0';
return n; // return j;
}
/*
gcc trim.c -o trim
./trim
trim(" \t\r\n \t\v\f\n"[10]): ""[0]
trim("\t \f \v\r\nHello \n\v\r\f \t"[19]): "Hello"[5]
trim("Hello"[5]): "Hello"[5]
*/





Note:  See Exercise_3-2 for function escape() and trimfile2.c in Exercise_1-18 for function trim().









Chapter_3     Exercise_3-6 BACK_TO_TOP goto     Chapter_4



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo