5 Replies - 5735 Views - Last Post: 25 December 2009 - 07:28 AM Rate Topic: -----

#1 SoetSout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-December 09

Increment alphabet

Post icon  Posted 24 December 2009 - 06:23 AM

ok this is no homework of mine just a lil side thing im working on.

i want to know is it possible to make a char or string value that contains a letter to increment it.

like i++ for an int value wil make it one more, but is that even possible for alphabetical values aswell??

i would understand if you guys see this as a possible homework. but im hoping you dont and help me on this
Is This A Good Question/Topic? 0
  • +

Replies To: Increment alphabet

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Increment alphabet

Posted 24 December 2009 - 06:34 AM

We will, for the moment assume we are only working with ASCII (i.e. not Unicode) which is an increasingly invalid assumption but let's assume it anyway.

As always with such questions the best thing to do is write some code.

So drop this in a simple main()
for( char i='a'; i<='z'; i++ ) {
	std::cout << i;
}



What happens?

The trade craft tip here is "when it doubt write some code"

Merry Christmas (or whatever it is for you)

This post has been edited by janotte: 24 December 2009 - 06:35 AM

Was This Post Helpful? 1

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Increment alphabet

Posted 24 December 2009 - 07:42 AM

Basically every single bit of data in a computer is a number. We get to define what that number may mean at different times...

One way of looking at binary data is as a string of ASCII characters. It turns out that most programming languages make this particular encoding very friendly.

For example in the code example provided by janotte above the compiler automatically converts 'a' and 'z' into the numbers 97 and 122. So his could could also have been written:
for( char i=97; i<=122; i++ ) {
		std::cout << i;
}

Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Increment alphabet

Posted 25 December 2009 - 02:46 AM

heh.. side thing? not another kler...
Was This Post Helpful? 0
  • +
  • -

#5 kapax  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 65
  • Joined: 02-July 08

Re: Increment alphabet

Posted 25 December 2009 - 05:15 AM

Possible. For CHARs it must be obvious -- depending on compiler you may or may not need to cast CHAR to INT. As for strings, the method is slightly more complicated -- first you must get an address of the specific character, using std::string's [] operator, then increase it (or do whatever else you want).
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Increment alphabet

Posted 25 December 2009 - 07:28 AM

View Postkapax, on 25 Dec, 2009 - 12:15 PM, said:

Possible. For CHARs it must be obvious -- depending on compiler you may or may not need to cast CHAR to INT.
Thankfully, this isn't compiler-dependent. a char is always guaranteed to be a 1-byte integral type; which means that the following is always well formed
int i = 'x'; 
char ch = i;
On a related note, functions such as toupper in the standard C library work with int rather than char.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1