Menghilangkan Spasi Pada String Dalam Pemograman C

Program try to neglecting the White spaces and blanks from given string to display only valid characters

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
 
int main()
{
   char text[100], blank[100];
   int c = 0, d = 0;
 
   printf("Enter some text\n");
   gets(text);
 
   while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c+1] == ' ')) {
        blank[d] = text[c];
        d++;
      }
      c++;
   }
 
   blank[d] = '\0';
 
   printf("Text after removing blanks\n%s\n", blank);
 
   return 0;
}

Contoh 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char* createpostslug( char slug[150] )
{
  char title[150];
  int c = 0, d = 0;

  printf("Enter post title : ");
  fgets(title, sizeof title, stdin);
  
  while (title[c] != '\0')
  {
    if (!(title[c] == ' ')) {
      slug[d] = tolower(title[c]);
      d++;
    } else {
      slug[d] = '-';
      d++;
    }
    c++;
  }

  title[d] = '\0';
  return slug ;
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus