How to clean the string?

This is a discussion on How to clean the string? within the C Programming forums, part of the General Programming Boards category; The strings stack again and again. I dunno how to clear and reset it. Code: #include <stdio.h> #include <string.h> #include ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    How to clean the string?

    The strings stack again and again. I dunno how to clear and reset it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
     
    int main()
    {
        char performance [23] = "Performance: ", performance1 [10], rate [3] ="", rate1 [3], yesno;
        int a;
    
    
        do
        {
        printf("Enter value: ");
        scanf(" %d", &a);
    
    
        
        if (a >= 2000)
        {
            strcpy (performance1,"Excellent");
            strncat (performance, performance1,9);
            strcpy (rate1,"7%");
            strncat (rate, rate1,3);    
        }
        
        else if (a>= 1000)
        {
        
            strcpy (performance1,"Good");
            strncat (performance, performance1,4);
            strcpy (rate1,"5%");
            strncat (rate, rate1,3);
        }
        
        else
        {
            strcpy (performance1,"Fair");
            strncat (performance, performance1,4);
            strcpy (rate1,"3%");
            strncat (rate, rate1,3);
        }
        
        printf("%s\n", performance);
        printf("%s\n", rate);
    
    
        printf("Continue (Y/N)? ");
        scanf(" %c",&yesno);
        }while (yesno == 'Y' || yesno == 'y');
     
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    What makes you think the string is dirty?
    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

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    If I say flush is that correct?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Heheh, that was a joke

    Anyway, my suggestion is that you declare those strings in the body of the do while loop. This way, they will be "reset" at the start of each iteration.

    Also, I have not checked, but be careful not to exceed the bounds of the array. You should also indent your code a little better, particularly the body of the loop.
    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

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Thank you very much. By the way, I dunno need to indent which line of code.

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
     
     
    int main()
    {
        char yesno;
    
    
        do
        {
        char performance [23] = "Performance: ", performance1 [10], rate [3] ="", rate1 [3];
        int a;
    
    
        printf("Enter value: ");
        scanf(" %d", &a);
    
    
        
        if (a >= 2000)
        {
            strcpy (performance1,"Excellent");
            strncat (performance, performance1,9);
            strcpy (rate1,"7%");
            strncat (rate, rate1,3);    
        }
        
        else if (a>= 1000)
        {
        
            strcpy (performance1,"Good");
            strncat (performance, performance1,4);
            strcpy (rate1,"5%");
            strncat (rate, rate1,3);
        }
        
        else
        {
            strcpy (performance1,"Fair");
            strncat (performance, performance1,4);
            strcpy (rate1,"3%");
            strncat (rate, rate1,3);
        }
        
        printf("%s\n", performance);
        printf("%s\n", rate);
    
    
        printf("Continue (Y/N)? ");
        scanf(" %c",&yesno);
        
        performance [23];
        rate [3];
        }while (yesno == 'Y' || yesno == 'y');
     
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    I might format your program like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        char yesno;
        do
        {
            char performance[23] = "Performance: ", performance1[10], rate[3] = "", rate1[3];
            int a;
    
            printf("Enter value: ");
            scanf(" %d", &a);
    
            if (a >= 2000)
            {
                strcpy(performance1, "Excellent");
                strncat(performance, performance1, 9);
                strcpy(rate1, "7%");
                strncat(rate, rate1, 3);
            }
            else if (a >= 1000)
            {
                strcpy(performance1, "Good");
                strncat(performance, performance1, 4);
                strcpy(rate1, "5%");
                strncat(rate, rate1, 3);
            }
            else
            {
                strcpy(performance1, "Fair");
                strncat(performance, performance1, 4);
                strcpy(rate1, "3%");
                strncat(rate, rate1, 3);
            }
    
            printf("%s\n", performance);
            printf("%s\n", rate);
    
            printf("Continue (Y/N)? ");
            scanf(" %c", &yesno);
    
            performance[23];
            rate[3];
        } while (yesno == 'Y' || yesno == 'y');
    }
    A few things to note:
    • By and large, you don't need the performance1 array. You can copy the string literal directly. In fact, your use of strncat is slightly off in each case because you fail to account for the null character, though you are saved by proper initialisation of the performance array.
    • Likewise for the rate1 array: it would be simpler to have used the string literal directly.
    • At the end of the body of the loop, you mysteriously access the performance and rate arrays out of bounds. Remove those lines.
    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

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    How to do?
    Last edited by ulti-killer; 06-11-2012 at 09:42 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    For example:
    Code:
    strcat(performance, "Excellent");
    You should be careful to ensure that the destination array has enough space for the concatenation. In this case, it has just enough space.
    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

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    I mean how to copy the string literal directly? I can't figure out a better solution.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    My example in post #8 is what I mean by copying the string literal directly, i.e., instead of first copying to another array, then using strncat with that other array.
    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

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Quote Originally Posted by laserlight View Post
    My example in post #8 is what I mean by copying the string literal directly, i.e., instead of first copying to another array, then using strncat with that other array.
    Thanks. Why i do a lot of redundant things?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me clean this up please.
    By Cstudent2121 in forum C Programming
    Replies: 19
    Last Post: 12-01-2005, 10:18 PM
  2. clean up?
    By nerore in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2003, 06:30 PM
  3. Help me clean this up.
    By uriel2013 in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2003, 10:50 PM

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