String manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicholas Graham

    #1

    String manipulation


    I'm writing a program that requires some string manipulations. Let's say
    I have a string

    s='x'

    Now, the ascii code for 'x' is 0x78. I want to be able to perform
    operations on this number, and print the character corresponding to the
    results of the operation. For example, the pseudo-code looks like:

    -read in string s from external file (for simplicity, we'll keep s='x')

    -determine the code for s (should be 0x78 or 120 in base 10), store it in
    variable c

    -add a literal to c (let's say c=c+1)

    -find out which character corresponds to the new code c=0x79

    -store the character in a new string s2

    At the end of this, I want s2='y'

    Any suggestions?

    NG

  • vincent wehren

    #2
    Re: String manipulation

    "Nicholas Graham" <nsgraham@ece.u alberta.ca> schrieb im Newsbeitrag
    news:Pine.LNX.4 .44.05041322372 50.11173-100000@feynman. ece.ualberta.ca ...
    |
    | I'm writing a program that requires some string manipulations. Let's say
    | I have a string
    |
    | s='x'
    |
    | Now, the ascii code for 'x' is 0x78. I want to be able to perform
    | operations on this number, and print the character corresponding to the
    | results of the operation. For example, the pseudo-code looks like:
    |
    | -read in string s from external file (for simplicity, we'll keep s='x')
    |
    | -determine the code for s (should be 0x78 or 120 in base 10), store it in
    | variable c
    |
    | -add a literal to c (let's say c=c+1)
    |
    | -find out which character corresponds to the new code c=0x79
    |
    | -store the character in a new string s2
    |
    | At the end of this, I want s2='y'
    |
    | Any suggestions?

    Take a look at the built-in functions ord() and chr() -- Chapter 2.1 of the
    manual.
    [color=blue][color=green]
    >> s = 'x'
    >> c = ord(s)
    >> c[/color][/color]
    120[color=blue][color=green]
    >> c+=1
    >> s2 = chr(c)
    >> s2[/color][/color]
    'y'

    --

    Vincent Wehren

    |
    | NG
    |


    Comment

    • Terry Reedy

      #3
      Re: String manipulation


      "vincent wehren" <vincent@visual trans.de> wrote in message
      news:d3ktct$h5m $1@news2.zwoll1 .ov.home.nl...[color=blue]
      > "Nicholas Graham"
      > | Any suggestions?[/color]
      [color=blue]
      > Take a look at the built-in functions ord() and chr()
      > -- Chapter 2.1 of the manual.[/color]

      And more generally, read all of Chap.2 of the Library Reference on builtin
      functions and types. Later chapters, most of them, are 'look up when you
      need to' type stuff, but C.2 is core material that everyone should read
      after the tutorial.

      Terry J. Reedy



      Comment

      Working...