Program to replace consecutive blank spaces with a single space.

This is a discussion on Program to replace consecutive blank spaces with a single space. within the C Programming forums, part of the General Programming Boards category; I am writing a program that should prompt the user to enter a line of text, and if there are ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Program to replace consecutive blank spaces with a single space.

    I am writing a program that should prompt the user to enter a line of text, and if there are consecutive spaces between the words, it should output the sentence with single spaces.

    I'm not really even sure that I'm headed in the right direction, but even if I am, my code has a lot of errors.

    Here's what I have:

    Code:
    int main(int argc, char **argv)
    {
     AnsiString; Line;
     int Index;
    
    Line = ReadStringPr("Enter your text with consecutive spaces." );
    Index = 1;
    
    
    while ((Index <= Length(Line) && Line[Index] == ' '))
    
    {
       if (Line[Index] != ' ')
          {
    
          WriteChar(Line[Index]);
          Index = Index + 1;
          }
       else
          {
    
          WriteChar(' ');
    
    
          while (Line[Index] == ' ')
            Index = Index + 1;
          }
    }
     getchar();
     return 0;
     }

    And here are the errors:

    C:\excredit5.cpp(3) : error C2065: 'AnsiString' : undeclared identifier
    C:\excredit5.cpp(3) : error C2065: 'Line' : undeclared identifier
    C:\excredit5.cpp(6) : error C2065: 'ReadStringPr' : undeclared identifier
    C:\excredit5.cpp(10) : error C2065: 'Length' : undeclared identifier
    C:\excredit5.cpp(10) : error C2109: subscript requires array or pointer type
    C:\excredit5.cpp(13) : error C2109: subscript requires array or pointer type
    C:\excredit5.cpp(16) : error C2065: 'WriteChar' : undeclared identifier
    C:\excredit5.cpp(16) : error C2109: subscript requires array or pointer type
    C:\excredit5.cpp(25) : error C2109: subscript requires array or pointer type
    C:\excredit5.cpp(29) : error C2065: 'getchar' : undeclared identifier
    Error executing cl.exe.


    I know that the errors are probably pretty self-explanitory, but I really don't know how to fix them. Can anyone help?


    Thanks so much.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your code looks like C -- next time, post in the C forum, not the C++ one.

    The first thing you need to do is to add this line to the top of your program.
    Code:
    #include <stdio.h>
    That should get rid of the getchar() error anyway.
    Code:
     AnsiString; Line;
    What are you trying to do here? Declare a variable called Line? Maybe you want to make its type char* instead.
    Code:
    char *Line;
    Code:
    WriteChar(' ');
    I'm not familiar with that function . . . try putchar(' ') instead.

    Code:
    Line = ReadStringPr("Enter your text with consecutive spaces." );
    There's no such function . . . maybe you want a printf() followed by a scanf()?

    That code is very incomplete. You're calling functions that don't exist. You're assuming that 1 is the first index of an array (0 is). What are you trying to do?

    Why don't you have a look at some tutorials? http://cprogramming.com/tutorial.html#ctutorial
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by dwks View Post
    That code is very incomplete. You're calling functions that don't exist. You're assuming that 1 is the first index of an array (0 is). What are you trying to do?
    If this is homework, maybe those functions are provided by a framework the instructor provides.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by brewbuck View Post
    If this is homework, maybe those functions are provided by a framework the instructor provides.
    Perhaps so. But the functions are so basic that the instructor should be advocating the use of putchar() etc instead of them.

    And even if they are provided, the OP is not including a header file or something, because of all the "undeclared identifier" errors.

    But it's worth a shot. Are you using any special libraries? Did your instructor (if you have one) give you a library to use?

    If you are, you'll have to set things up so you can use those functions. You could try putting the library in the same directory as your source code, #include-ing whatever .h files are part of the library, and linking to the .lib file (since you seem to be using MSVC). I'm not sure how to do this with MSVC, but I'm sure you could figure it out with some googling -- or someone who uses MSVC could tell you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check for blank spaces in a string
    By BENCHMARKMAN in forum C Programming
    Replies: 19
    Last Post: 03-12-2008, 06:14 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  4. Detecting two consecutive spaces in string
    By bob2509 in forum C++ Programming
    Replies: 20
    Last Post: 04-22-2002, 01:48 PM
  5. removing blank spaces
    By Bash in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 09:37 AM

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