trimg.c
K&R, p. 65
(trim with gotos)
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--)
{
switch (s[n])
{
case ' ' : case '\t' : case '\n' :
case '\f' : case '\v' : case '\r' :
break; // continue; // go to next for() iteration
default:
goto out1; // break would just exit switch() and continue
} // with the next for() iteration
}
out1: // first label for goto
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++)
{
switch (s[i])
{
case ' ' : case '\t' : case '\n' :
case '\f' : case '\v' : case '\r' :
break; // continue; // go to next for() iteration
default:
goto out2; // break would just exit switch() and continue
} // with the next for() iteration
}
out2: // second label for goto
// 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 trimg.c -o trimg
./trimg
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]
*/
Comments
Post a Comment