Tuesday 3 July 2012

Remove spaces between substrings of string in C


Code - 


/*__Program to remove two consecutive spaces between various substrings of a string___*/

//___________Inlcuding header files___________

#include<stdio.h>

#include<conio.h> //_________Mainly included for the getch function_____________

#include<string.h> //_________Mainly included for gets() function used to scan a string___________

int main()
{
char original_string[20];

int i = 0, j = 0;

printf("\n\n  __ Program to remove consecutive spaces between sub-strings of a string __");

printf("\n\n\n\n\t   Enter the String - ");

gets(original_string);

for(i = 0; original_string[i] != '\0'; i++)
{
if (original_string[i] == 32)
{
if (original_string[i+1] == 32)
{
for (j = i; original_string[j] != '\0'; j++ )
{
original_string[j] = original_string[j+1];
}

i = i - 1;
}
}

}

printf("\n\n\t       Final String - ");

puts(original_string);

getch();
}

Follow Us on Facebook - Assignment Hub

No comments:

Post a Comment