Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Friday, May 30, 2014

C program remove spaces, blanks from a string

C programming code

#include <stdio.h>
#include<conio.h> 
void main()
{
   char text[100], blank[100];
   int c = 0, d = 0;
   clrscr();
   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);
  getch();
}

Output of program:


C programming code using pointers
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define SPACE ' '
void main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;
   clrscr();
   printf("Enter a string\n");
   gets(string);
   length = strlen(string);
   blank = string;
   start = (char*)malloc(length+1);
   if ( start == NULL )
   exit(EXIT_FAILURE);
   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
d++;
      }
      c++;
   }
   *(start+d) = '\0';
   printf("%s\n", start);
   free(start);     
   getch();
}

No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC