469,306 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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

Jul 18 '05 #1
2 1577
"Nicholas Graham" <ns******@ece.ualberta.ca> schrieb im Newsbeitrag
news:Pi**************************************@feyn man.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.
s = 'x'
c = ord(s)
c 120 c+=1
s2 = chr(c)
s2

'y'

--

Vincent Wehren

|
| NG
|
Jul 18 '05 #2

"vincent wehren" <vi*****@visualtrans.de> wrote in message
news:d3**********@news2.zwoll1.ov.home.nl...
"Nicholas Graham"
| Any suggestions? Take a look at the built-in functions ord() and chr()
-- Chapter 2.1 of the manual.


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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by siddhartha mulpuru | last post: by
9 posts views Thread by mjakowlew | last post: by
4 posts views Thread by Jim McGivney | last post: by
7 posts views Thread by John A Grandy | last post: by
4 posts views Thread by WaterWalk | last post: by
5 posts views Thread by Cleverbum | last post: by
3 posts views Thread by crprajan | last post: by
3 posts views Thread by frankeljw | last post: by
1 post views Thread by adam bob | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.