Thread: string question

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    17

    Question string question

    hi there,

    i am trying to get all different possibilites of my string but i get lost when get deep in it.

    assume
    char *s = "dummy"
    but there may be substitutions for specifici characters.
    e.g. (u-f) (u-z) (y-w)

    so all possible strings are:
    dummy dummw
    dfmmy dfmmw
    dzmmy dzmmw

    i thinik algorithm is a recursion.
    any suggestions?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use strchr().
    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
    Registered User
    Join Date
    May 2003
    Posts
    17
    how? i could not get the point

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *s = "dummy"
    Except you can't modify such strings, so you would at least need to start with
    char s[] = "dummy";

    > how? i could not get the point
    strchr() returns a pointer to a char, if it finds the char, or NULL if not.
    So
    char *p = strchr(s,'m');
    Would result in p pointing to the first 'm'

    You can then do something like
    *p = 'z';

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To use strchr() to replace all occurences of a character, you can use a loop:
    Code:
    char *p;
    while(p = strchr(s, 'a')) *p = 'b';
    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.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    17
    thanks i see that but there may be more than one substitution so i need a combination or st. like that.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well what would be quite helpful is for you to make your best attempt at the code. Then, if it is not working the way you want, post it here and we can advise you as necessary.

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM