how to change commas with exclamation marks in a string ?

This is a discussion on how to change commas with exclamation marks in a string ? within the C Programming forums, part of the General Programming Boards category; hi, I have searched all kind of collate algorithms, but havent found nothing suitable for that kind of operation. Could ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    4

    how to change commas with exclamation marks in a string ?

    hi, I have searched all kind of collate algorithms, but havent found nothing suitable for that kind of operation. Could somebody help? How is it possible to make distinctions of commas and other charackters?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Look around the string.h library for solutions. I know there are a couple of notable functions that find stuff in C-style strings, like strchr(). From there it's merely just changing whatever the function returns to an !

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why don't you just go through all the string and check each character ?
    Code:
    char string[] = "This, mister, is a message.";
    for(int i = 0; string[i] != '\0'; i++)
        if(string[i] == ',')
            string[i] = '!';

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    4
    Quote Originally Posted by Desolation
    Why don't you just go through all the string and check each character ?
    Code:
    char string[] = "This, mister, is a message.";
    for(int i = 0; string[i] != '\0'; i++)
        if(string[i] == ',')
            string[i] = '!';
    ok, but what does that
    Code:
    != '\0'
    part do?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Code:
      string[i] != '\0' /* true if character i in string is not a terminating '\0' */
    But Desolation is implying that my earlier solution (using a standard library function) is difficult. It is not.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
        char string[] = "hey, this is a simple message,,";
        char *pch = NULL;
        while (pch = strchr(string, ','))
            *pch = '!';
        puts(string);
        return 0;
    }
    Last edited by whiteflags; 05-23-2006 at 09:29 AM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    4
    at first I tried in this way as Desolation told and it seems to work, but i have one small problem: How should the output(sentence where commas are replaced with exclamation marks) look like?

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    char string[] = "hey, this is a simple message,,";
    for(int i = 0; string[i] != '\0'; i++)
          {
                   if(string[i] == ',')
                   {
                      string[i] = '!';
                   }
             
           
          }
    printf (
    */I woult like to have a sentence here, where commas are replaced with exclamation marks*/
    );
    
    getchar();
    getchar();
    return 0;
    }
    Last edited by m4rtin; 05-23-2006 at 09:56 AM.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Uhm, use puts(string);

    If you really insist on using printf, you can, even though you don't need it, but here's how.
    Code:
    printf("%*s\n", strlen(string), string);
    I say you don't need printf because that prints out a format string, and you don't really need this type of work. Printing strings is already done better by other stdio functions like puts and fputs
    Last edited by whiteflags; 05-23-2006 at 09:59 AM.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    most basic question I've seen this weak, and there have been
    quite a few...

    printf ("%s", string);
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    4
    ofcourse Thanx for your support!!

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    printf ("%s", string);
    Just as an aside, you shouldn't use %s by itself. Dave pointed this out to me when he posted this article.
    http://en.wikipedia.org/wiki/Format_string_attack

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It's that you shouldn't do this:
    Code:
    printf(str);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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