473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Extracting char from string

1
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 4167
boxfish
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.
  4.  
Or do you already know this and are trying to do something else?
Sep 18 '08 #2
gpraghuram
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.

Raghu
Sep 18 '08 #3
Ganon11
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
weaknessforcats
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

7
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...
2
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...
2
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...
6
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. .. .. ..
1
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...
2
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...
13
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...
4
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
16
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...
2
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? ...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.