Advertisement
Mazamin

Remove multiple/start/end spaces

Jan 4th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #define MAX_STR_LEN 64
  5.  
  6. int main(){
  7.     char ch, string[MAX_STR_LEN], newstr[MAX_STR_LEN];
  8.     int n=0, i, j=0;
  9.     while((ch=getchar())!='$')
  10.         string[n++]=ch;
  11.     string[n]='\0';
  12.     for(i=0;i<n;i++){
  13.         if(isalnum(string[i]))
  14.             newstr[j++]=string[i];
  15.         else if(((isspace(string[i]))&&(isalnum(string[i+1])))&&(j!=0))
  16.             newstr[j++]=' ';
  17.     }
  18.     newstr[j]='\0';
  19.     printf("The resulting string is:\n%s\n", newstr);
  20.     return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement