Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <conio.h>
- #define MAXSIZE 1000
- void TrimLeft (char s[]) {
- char *p = s;
- while (isspace(*p))
- ++p;
- strcpy(s, p);
- return;
- }
- void TrimRight (char s[]) {
- int i = strlen(s) - 1;
- while (isspace(s[i]))
- --i;
- s[i+1] = '\0';
- return;
- }
- void TrimMiddle (char s[]) {
- int i = 0, n = strlen(s), numSpace;
- // if (isspace(s[0]))
- // numSpace = 1;
- // else
- // numSpace = 0;
- for (i = 1; i <= n; ++i) {
- if (isspace(s[i]) && isspace(s[i-1])) {
- ++numSpace;
- } else {
- s[i-numSpace] = s[i];
- }
- }
- // if (isspace(s[n-numSpace-1]))
- // s[n-numSpace-1] = '\0';
- // Must use TrimRight because have a trailing space
- }
- void Std (char s[]) {
- int i;
- for(i=0;i<strlen(s);i++)
- {
- if(s[i]==' '&&s[i+1]==' ') {
- strcpy(&s[i],&s[i+1]);
- i--;
- //printf("@%s@\n", s);
- }
- }
- }
- int main () {
- char s[MAXSIZE];
- printf("Enter a string:\n");
- scanf("%[^\n]", s);
- //Std(s);
- TrimLeft(s);
- printf("The string after trim left is:\n%s\n", s);
- TrimRight(s);
- printf("The string after trim right is:\n%s\n", s);
- TrimMiddle(s);
- printf("The string after trim middle is:\n%s\n", s);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement