Results 1 to 3 of 3

Thread: Removing gap in string in C

  1. #1
    PerezMorris is offline Senior Member
    Join Date
    Dec 2009
    Posts
    250
    Rep Power
    3

    Default Removing gap in string in C

    Hi. I am last year MCA student. I am learning C programming language. I want to write program in C language but there is problem with difficulty of spaces in string. I want to delete spaces from string. If you have any in code please let me know that. Thanks in advanced.

  2. #2
    HallMiller is offline Senior Member
    Join Date
    Dec 2009
    Posts
    251
    Rep Power
    3

    Default

    Refer following program for removing space in string in C. Hope this helps you out with your problem:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    void main() {
      char ch[80] = "Where there is will, there is a way.";
      char *p1 = ch;
      char *p2 = ch;
      p1 = ch;
       while(*p1 != 0) {
        if(ispunct(*p1) || isspace(*p1)) {
          ++p1;
        }
        else
        *p2++ = *p1++; 
      }
      *p2 = 0; 
      printf("nAfter removing the spaces, string is:%sn", ch);
      getch();
    }

  3. #3
    AllenBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    240
    Rep Power
    3

    Default

    The code that I have mentioned below will definitely help you. It is very simple code.

    Code:
    #include<stdio.h>
    int main(void)
    {
    char p[] = "please remove space from me";
    int index = 0;
    
    while(p[index] != '\0')
    {
    if (p[index] == ' '){
    p[index] = p[index+1];
    p[index+1] =' ';
    }
    index++;
    }
    puts(p);
    return 0;
    }

Similar Threads

  1. Removing space in string in C
    By MoralesMyers in forum Programming
    Replies: 2
    Last Post: 04-09-2010, 03:27 PM
  2. Convert string into DateTime
    By Bernie Schwartz in forum Programming
    Replies: 3
    Last Post: 03-05-2010, 10:19 AM
  3. Converting PHP String into Date
    By frank_clark in forum Programming
    Replies: 0
    Last Post: 01-08-2010, 05:27 PM
  4. String
    By quckhil in forum Programming
    Replies: 0
    Last Post: 08-22-2008, 09:32 AM
  5. String
    By techno23 in forum Programming
    Replies: 0
    Last Post: 03-26-2008, 12:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48