Removing spaces

This is a discussion on Removing spaces within the C Programming forums, part of the General Programming Boards category; I have written the program (see below) which is supposed to take a number from a .txt file but the ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Removing spaces

    I have written the program (see below) which is supposed to take a number from a .txt file but the number may or may not have spaces in between the numbers. I am trying to remove the spaces and then make the number so that I can later perform calculations on it.

    E.g. if the input is: 1 . 456 7 7
    then the output should be: 1.45677

    I am having problems with the "remove" section of my program (lines 32 to 37). Can anyone help?
    Code:
    #include <stdio.h>
    main()
    
    // open the file
    {
    FILE*fp;
    int letter;
    if((fp=fopen("quote.txt","r"))==NULL)
    {
    puts("Cannot open the file");
    }
    
    //declarations
    int i;
    i=0;
    char a[20];
    int j;
    char b[20];
    int k=0;
    
    //read each char from file
    while((letter=fgetc(fp)) !=EOF)
    {
    	printf("%c",letter);
    a[i] = letter;
    i++;
    }
    
    printf("\n");
    
    //remove spaces
    for (j=0; j<20; j++) {
    	if (a[j] == ' ') {b[k] = a[j]; k++;}
    
    	printf("%c", b[k]);
    }
    
    //output the result in form ready to do mathematical manipulations
    printf("\n");
    printf("%s", b);
    fclose(fp);
    return 0;
    }
    Last edited by chris1985; 12-22-2004 at 07:42 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Please use [code][/code]Tags

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I am having problems with the "remove" section of my program (lines 32 to 37). Can anyone help?
    So what exacty is your problem with it ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    If I were you I would just drop the spaces while reading the file, but as far as your remove spaces section I think you just need to reverse the conditional. Also, don't forget to null-terminate your strings.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  2. Replies: 1
    Last Post: 03-08-2005, 11:02 AM
  3. Removing spaces from a "string"
    By sytaylor in forum C++ Programming
    Replies: 20
    Last Post: 03-25-2004, 09:14 AM
  4. k&r ex1-18 removing trailing spaces.
    By xion in forum C Programming
    Replies: 1
    Last Post: 07-14-2003, 02:20 PM
  5. Removing spaces from strings
    By PunkyBunny300 in forum C Programming
    Replies: 6
    Last Post: 02-21-2003, 01:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21