472,962 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,962 software developers and data experts.

Extracting char from string

Need to prompt user for text, count occurrence of each alphabetic character, then sort and print in descending order. I'm using two parallel arrays, one for characters, one for occurrence of each, I know how to sort with a for loop. Don't know how to extract from string.

Use something like
while (cin >> s)

Any suggestions?

Program needs to read text until terminated by pressing <enter><ctrl+d>. This is all in a Unix environment, code is C++, if it matters.
Sep 18 '08 #1
4 4154
469 Expert 256MB
You can use the indexing operator, []. myString[i] will give you the character in myString at position i; is this what you're looking for?
For example:
Expand|Select|Wrap|Line Numbers
  1. string myString = "byeworld";
  2. cout << myString[0] << endl; // Prints out b.
  3. cout << myString[1] << endl; // Prints out y.
Or do you already know this and are trying to do something else?
Sep 18 '08 #2
1,275 Expert 1GB
You can get the C string from the string using .c_str() and then work on it as a normal C string.

Sep 18 '08 #3
3,652 Expert 2GB
Are you allowed to use a map? This sort of task would work very well for a map.

Alternatively, you could find a way to use only one array to maintain the count of each letter. You'd have to use the array indexes to indicate which letter you had, though.
Sep 18 '08 #4
9,208 Expert Mod 8TB
Don't know how to extract from string.
You don't need to do this.

I asume your array of counts has been initialized to 0.

All you have to do this look at each character of the string and add 1 to the correct count.
Sep 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

by: cpp_weenie | last post by:
Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters...
by: Aleander | last post by:
Hi! I have to write the records of a vector in a file, e and then open this file to extract the record to refill the vector. My program has two class: Visita(Appointment) and Data(date). The...
by: deanfamily11 | last post by:
I have a program that gets a date from the user in the format dd/mm/yy, and I need to extract the d's, m's, and y's to be used within the program. I have the program get the date as a string. How...
by: Kifah Abbad | last post by:
Ok guys, here is the deal, i hope someone can help me out with this. I receive a string through a socket...i dunno how long this string is, but i save it all into a buf. .. .. ..
by: andy.lee23 | last post by:
Hi, I've been learning C for a couple of weeks and I need some help. I have a text file and I want to extract integers from it to perform calculations. The format of the file is as follows: 3...
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
by: Pilcrow | last post by:
This problem was raised in comp.lang.perl.misc, and the poster was concerned, among other things, by the speed of execution. Since C is faster than perl, I wonder how a C coder would solve it? ...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.