Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,156 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,126 people online right now. Registration is fast and FREE... Join Now!




String question for app

 
Reply to this topicStart new topic

String question for app

lockdown
16 Dec, 2007 - 03:19 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Is it possible to look at each individual character that a user enters in a string. From what I can remember a string is seen as a whole and the contents of the string cant be broken down.

An example I am trying to do is like if I enter my name into my program (Evan). I want to be able to look at it as each letter individually so it would be E,V,A,N and work with each one. Is that possible to do in a string?

User is offlineProfile CardPM
+Quote Post

kdbolt70
RE: String Question For App
16 Dec, 2007 - 03:39 PM
Post #2

New D.I.C Head
*

Joined: 16 Dec, 2007
Posts: 19


My Contributions
QUOTE(lockdown @ 16 Dec, 2007 - 06:19 PM) *

Is it possible to look at each individual character that a user enters in a string. From what I can remember a string is seen as a whole and the contents of the string cant be broken down.

An example I am trying to do is like if I enter my name into my program (Evan). I want to be able to look at it as each letter individually so it would be E,V,A,N and work with each one. Is that possible to do in a string?



You are able to access chars in a string simply by the subscript operator:

CODE
string name = "Evan";
char c = name[0];
cout << c;


Also, at(int) will return the character at that position.

This post has been edited by kdbolt70: 16 Dec, 2007 - 03:40 PM
User is offlineProfile CardPM
+Quote Post

lockdown
RE: String Question For App
16 Dec, 2007 - 05:12 PM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So thats similar to loading the string into a array correct? So each character turns into a element of a array?

User is offlineProfile CardPM
+Quote Post

Bench
RE: String Question For App
16 Dec, 2007 - 06:35 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(lockdown @ 17 Dec, 2007 - 01:12 AM) *

So thats similar to loading the string into a array correct? So each character turns into a element of a array?

Underneath the cute string interface is nothing more than an array of characters; so its not a case of "turns into" - The string interface just exposes some of that raw implementation detail to you - Enough to be useful, but not enough for you, the programmer, to burn your fingers with it by doing something "bad" (Well, maybe a little, but far less than when you start playing with "raw" arrays and pointers).

I like to think of the C++ string type as a "container" specially designed for characters. Much in the way that a vector can hold multiple items of data of a certain type, except that a C++ string only ever stores characters, but allows easy manipulation of so-called "plain text".
User is offlineProfile CardPM
+Quote Post

lockdown
RE: String Question For App
16 Dec, 2007 - 07:44 PM
Post #5

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(Bench @ 16 Dec, 2007 - 07:35 PM) *

QUOTE(lockdown @ 17 Dec, 2007 - 01:12 AM) *

So thats similar to loading the string into a array correct? So each character turns into a element of a array?

Underneath the cute string interface is nothing more than an array of characters; so its not a case of "turns into" - The string interface just exposes some of that raw implementation detail to you - Enough to be useful, but not enough for you, the programmer, to burn your fingers with it by doing something "bad" (Well, maybe a little, but far less than when you start playing with "raw" arrays and pointers).

I like to think of the C++ string type as a "container" specially designed for characters. Much in the way that a vector can hold multiple items of data of a certain type, except that a C++ string only ever stores characters, but allows easy manipulation of so-called "plain text".


Thanks - Yeah after kdbolt70 showed me an example I was like hmm thats like a vector. So all I need to do is uses the subscript to get the data I want out of the string.

Also just did some reference work for the string class and everything I wanted to do is now possible. I was working on a simple encryption program and wanted to take text user enters and encrypt that. It is pretty simple math equation to do this so far each letter you add 1 and then that is saved as the new text. I am doing it just for fun and also since I am studying some encryption methods in my security course. I am working on a more complex verison of it on paper were my math equation is much more complicated. But it will take awhile to get then in development because I am finding some issues with my own math smile.gif

This post has been edited by lockdown: 16 Dec, 2007 - 08:29 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:20PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month