Read a String Value and Ignore Spaces in C


C Program to Remove Spaces from Cord

In this commodity, you lot will learn and get code to remove spaces from string using C program. Here are the list of programs available in this article:

  1. Remove all Spaces from a Cord entered past User
  2. Remove simply Extra Spaces from a Cord entered by User

The commencement programme removes all the spaces from a given string in C. For instance, if the given string is

Then the cord subsequently removing all the spaces volition be:

Whereas the second programme removes only actress spaces from the given string. For case, if the string entered by user is:

And so after removing only extra spaces from this string, the new cord volition exist:

Remove All Spaces from Cord

To remove or delete spaces from string in C programming, yous have to inquire from user to enter a cord. Now beginning checking for spaces. If space gets found at any index, and then but shift all the forward character to ane index back as shown in the program given below. The question is, write a program in C that removes all the spaces from a given string by user at run-time. The answer to this question is given below:

            #include<stdio.h>            #include<conio.h>            int            main() {            char            str[l];            int            i=0, j, chk;            printf("Enter a String: ");            gets(str);            while(str[i]!='\0')     {         chk=0;            if(str[i]==' ')         {             j=i;            while(str[j-1]!='\0')             {                 str[j] = str[j+1];                 j++;             }             chk = 1;         }            if(chk==0)             i++;     }            printf("\nString (without spaces):            %south", str);            getch();            return            0; }

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program remove spaces from string

Now supply whatever string and press ENTER key to see the same string without space. Hither is the second snapshot of the sample run:

c remove spaces from string

Program Explained

Now let's discuss virtually some of the principal steps used in in a higher place programme with taking an example of the string shown in previous sample run's snapshot:

  • The string gets stored in str variable in a way that
    • str[0] holds West
    • str[1] holds e
    • str[2] holds l
    • and then on
  • Because, spaces present is at index no. seven, 10, 11, 12, 13, 14. So
    • str[7], str[10], str[11], str[12], str[13], str[xiv] holds space
  • So when the value of i becomes 7, then the status of first if block evaluates to exist true. That is, space gets institute at 7th index.
  • And so the value of i gets initialized to j and the shifting of all forwards characters (from where the space gets plant) to ane index dorsum, gets performed. Because, currently the space gets constitute at index number seven. And so
    • The grapheme at index no.8 moved to index no.seven
    • The character at index no.9 moved to index no.8
    • The character at alphabetize no.10 moved to index no.9
    • and so on
    • until the last character of the cord
  • Because the value of i is 7 right at present, and the character was at alphabetize no.8 gets moved back to the index no.7
  • Therefore here the value of i does not gets incremented, because for now the grapheme present at index no. 7 is unknown
  • That is, we take to check it, whether it is a character or a infinite
  • And if no infinite gets plant at current index, and then just increment the value of i and bank check for graphic symbol nowadays at adjacent alphabetize number
  • Go along the process, until the last grapheme

Remove Extra Spaces from Cord

Now this plan removes just actress spaces from the string. That is, if there are 2 or more spaces available between whatsoever two words in the string, then after executing this program, there volition just i space left between all words.

            #include<stdio.h>            #include<string.h>            #include<conio.h>            int            main() {            char            str[200];            int            i, j, len;            printf("Enter a String: ");            gets(str);     len =            strlen(str);            for(i=0; i<len; i++)     {            if(str[0]==' ')         {            for(i=0; i<(len-1); i++)                 str[i] = str[i+1];             str[i] =            '\0';             len--;             i = -1;            continue;         }            if(str[i]==' '            && str[i+1]==' ')         {            for(j=i; j<(len-one); j++)             {                 str[j] = str[j+ane];             }             str[j] =            '\0';             len--;             i--;         }     }            printf("\nNew String =            %s", str);            getch();            return            0; }

Here is its sample run:

remove extra spaces from string c

Same Program in Other Languages

  • C++ Remove Spaces from String
  • Java Remove Spaces from Cord
  • Python Remove Spaces from String

C Online Exam


« Previous Program Next Programme »




lynchscland.blogspot.com

Source: https://codescracker.com/c/program/c-program-remove-spaces-from-string.htm

0 Response to "Read a String Value and Ignore Spaces in C"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel