help! string literals

This is a discussion on help! string literals within the C Programming forums, part of the General Programming Boards category; I am new to C programming, i am currently working on a program where i must accept keyboard input and ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Question help! string literals

    I am new to C programming, i am currently working on a program where i must accept keyboard input and reproduce the text after eliminating white space. unless the text is in a string literal, then it must be reproduced with spaces. i have the first part (printing without spaces) but im not quite sure how to make it print normally within string literals....can anyone help me? here is my code

    Code:
    #include <stdio.h>
    
    int main()
    {
       int c;
       
       printf("This program eliminates whitespace from an input stream,\n");
       printf("except from within string literals in double quotes.\n");
       printf("\n");
       printf("Type characters, and enter EOF after <enter> to finish:\n");
       
       while ((c = getchar()) != EOF) 
          if ( c != ' ') 
          putchar(c);
          
       system("pause");
       return 0;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    What about your previous thread? cyberfish gave you a nice answer.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    the answer cyberfish gave is to advanced for what i am trying to do. i mean its probably not that complicated...but we have only just learned about file copying, word counting, line counting and arrays. i can't use bool.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Can you provide an example of a string literal and one that is not.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    ok sorry maybe i'm not using the correct terminology, when i say
    hi i am new to c programming......i need it to print hiiamnewtocprogramming........this is what my program currently does....but additionally, when i use quotes and say......."hi i am new to c programming" i need it to print exactly that "hi i am new to c programming"

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    haha i guess you go to fccj to huh?? I'm having the same problem

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by cakestler
    the answer cyberfish gave is to advanced for what i am trying to do. i mean its probably not that complicated...but we have only just learned about file copying, word counting, line counting and arrays. i can't use bool.
    I think that cyberfish's answer is straightforward. Instead of using bool, use an int. You do not have to worry about counting... all you need to worry about is: do I ignore whitespace, or not? For that, you need to know if you are currently processing a string literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you don't need a flag. test each character and loop through literals.
    Code:
    char *quoted_ws(char *s)
    {
        char *p = s, *q = s;
        while (*p)
            if (!*q) *p = '\0';
            else if (isspace(*q)) ++q;
            else if ((*p++ = *q++) == '"') while ((*p++ = *q++) != '"') ;
        return s;
    }
    easy.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Meldreth View Post
    you don't need a flag. test each character and loop through literals.
    Code:
    char *quoted_ws(char *s)
    {
        char *p = s, *q = s;
        while (*p)
            if (!*q) *p = '\0';
            else if (isspace(*q)) ++q;
            else if ((*p++ = *q++) == '"') while ((*p++ = *q++) != '"') ;
        return s;
    }
    easy.
    Easy to crash this program you mean? just pass the string with one quote in it...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,605
    And perhaps a little more advanced solutions, as well...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Easy to crash this program you mean? just pass the string with one quote in it...
    it's a homework assignment, not a display routine in the stock exchange. but if you want to nitpick then the flag solution won't work either because if it sees a quote it won't know if the quote is terminated or not. the output will be wrong unless you write a case to find the string literals first and figure out how to handle an odd number of them.
    Code:
    char *quoted_ws(char *s)
    {
        int quotes[256] = {0};
        int n = 0;
        int i, j, k = 0;
        for (i = 0; s[i]; i++) if (s[i] == '"') quotes[n++] = i;
        for (i = 0, j = 0; s[i]; )
        {
            if (!s[j]) s[i] = '\0';
            /* ignore the last unpaired quote */
            else if (j == quotes[k] && k < n)
            {
                while (j <= quotes[k+1]) s[i++] = s[j++];
                k += 2;
            }
            else if (isspace(s[j])) ++j;
            else s[i++] = s[j++];
        }
        return s;
    }
    i think you'll agree that this is a little too advanced to be an assignment for someone new to c.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    but if you want to nitpick then the flag solution won't work either because if it sees a quote it won't know if the quote is terminated or not
    It does not need to know.

    There is no requirement to work "correct" on incorrect input.

    But any program has an absolute requirement - It cannot crash no matter what user input it receives
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by vart View Post
    It does not need to know.

    There is no requirement to work "correct" on incorrect input.

    But any program has an absolute requirement - It cannot crash no matter what user input it receives
    that's stupid. a program absolutely cannot crash, but it's ok if it gives wrong results as if they were right. hypocrites are not allowed to criticize my methods until they correct their own.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    but it's ok if it gives wrong results as if they were right
    Maybe you should learn to read before you start interpreting my saying?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by vart
    There is no requirement to work "correct" on incorrect input.

    But any program has an absolute requirement - It cannot crash no matter what user input it receives
    you think a program can give incorrect results on incorrect input but shouldn't ever crash. i don't see any other way to interpret this and i say you're a hypocrite because you condemn crashing but allow an even worse situation. silently wrong output can be mistaken for correct output, a crash can't be overlooked.

    if you didn't mean what you wrote, you should learn to write, smartass.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. How to concatenate, then widen string literals
    By lonehacker in forum C Programming
    Replies: 13
    Last Post: 09-15-2004, 10:56 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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