473,836 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding # of spaces in string to map

19 New Member
hello,

This is my 1st post here!

*welcome drjay*

Thanks! I look answering questions and getting answers to other!

Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:

string = "hello world! hello world... I'm a live"

map<string,int>

hello 2
wold 2
! 1
. 3
Im 1
' 1
a 1
live 1
6

This is how the output should look like when the map is looped/traversed...

This is part of a bigger project... this is how I tackle word and punctuations.

Read the string, call method on the string that removes all punctuations. call a second method on the original string to filter all punctuations.

split the two strings using the space as delimiter/splitter, add to a vector<string> (this vector will obviously have repeats).

call method addtomap(vector ,map)
loop through the vector
check if vector[i] is there in the map
if found second++
else insert(vector[i],1)

the way i have it setup for spaces is i call a method that returns an int, the number of spaces.

create a new string of size 'number of spaces'
string spaceLine;
int noofspaces;
spaceLine.inser t(0, noofspaces, ' ');

basically SpaceLine is a string of spaces.

then i call a method addSpacesToMap( string,map)
loop through the string, check if " " is present in the map
if found second++
else insert(" ",1)
=============== =========error= =============== =============
test1.cpp: In function `void addSpacesToMap( std::string, std::map<std::s tring, int, std::less<std:: string>, std::allocator< std::pair<const std::string, int> > >&)':
test1.cpp:96: error: invalid conversion from `char' to `const char*'
test1.cpp:96: error: initializing argument 1 of `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc = std::allocator< char>]'
=============== =========error= =============== =============

=============== ==my code =============== =============== ===
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

typedef map<string,int> ::iterator itermap;

string parsingForPunct uations(string line);
int parsingForSpace s(string line);
void addToMap(const vector<string> &word, map<string,int> &map);
void addSpacesToMap( const string &space, map<string,int> &map);

int main(){
map<string,int> mymap;
string str = "Hello! hello world... I'm I'd @ 12+3 (hello)";
//string newstr = parsingForPunct uations(str);
int noofspaces = parsingForSpace s(str);
cout << "str: " <<str<<endl;
//cout << "newstr: " <<newstr<<end l;
cout << "# of spaces: " <<noofspaces<<e ndl;

string spaceLine;
vector<string> tempVec;

tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck("apple");
tempVec.push_ba ck("cat");
tempVec.push_ba ck("hello");
tempVec.push_ba ck("world");
tempVec.push_ba ck(" ");
tempVec.push_ba ck(" ");

spaceLine.inser t(0, noofspaces, ' ');
cout << "Spaces Line:" <<spaceLine<<"e ndl"<<endl;

for(int i = 0 ; i < tempVec.size(); i++){
cout << tempVec[i]<<endl;
}
addToMap(tempVe c,mymap);
addSpacesToMap( spaceLine,mymap );

return 0;
}

string parsingForPunct uations(string line){
string str;
for(int i = 0 ; i<line.size() ; i++){
char j = line[i];
if(j>=33 && j<=47){
str = str + " " +line[i];
}
}
return str;
}

int parsingForSpace s(string line){
int spaces = 0;
for(int i = 0 ; i<line.size() ; i++){
spaces += (line.at(i)==' ');
}
return spaces;
}

void addToMap(const vector<string> &word, map<string,int> &map){

itermap it;

for(int i = 0 ; i < word.size() ; i++){
it = map.find(word[i]);
if(it!=map.end( )){
it->second++;
}else{
map.insert(pair <string,int>(wo rd[i],1));
}
}
}
void addSpacesToMap( string space, map<string,int> &map){

itermap it1;

for(int i = 0 ; i < space.length() ; i++){
it1 = map.find(space[i]);
if(it1!=map.end ()){
it1->second++;
}else{
map.insert(pair <string,int>( " ",1));
}
}
}
=============== =======my code=========== =============== ==

if you comment out the addSpacesToMap( ), program will compile. also this just a segment of my program...

this is probably the longest 'question' here... but if you get to this point and you are reading this, you only need to help me out with one method and one error:
error: invalid conversion from `char' to `const char*'

thanks in advance

drjay
Nov 21 '08 #1
8 4745
boxfish
469 Recognized Expert Contributor
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.
Nov 21 '08 #2
drjay1627
19 New Member
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.
I'll try that mate thanks!
Nov 21 '08 #3
donbock
2,426 Recognized Expert Top Contributor
In your example the substring "I'm" is broken into the following words:
> one instance of single-quote (')
> one instance of "Im".

Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.
Nov 21 '08 #4
drjay1627
19 New Member
In your example the substring "I'm" is broken into the following words:
> one instance of single-quote (')
> one instance of "Im".

Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.
This is not my assignment mate... yeah this is how my instructor wants... basically I making a word frequency map to be encoded using a huffman code.

project is read in a very large text file, and encode it.
Nov 21 '08 #5
drjay1627
19 New Member
space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
Expand|Select|Wrap|Line Numbers
  1. space.substr(i, i + 1)
I don't know if this will make your code work correctly, but it will clear up the error.
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
Hope this helps.

i did what you said this is the output i get:

vector "hello"
vector "world"
vector "apple"
vector "cat"
vector "hello"
vector "world"
vector " "
vector " "
vector " "
vector " "
vector " "
vector " "
vector " "
map 2
map 2
map 2
map 1
map apple 1
map cat 1
map hello 2
map world 2

i changed it a bit. rather than calling a method addSpaceToMap() i loop through the string of spaces and i add to tempVec (if you remember this is a vector).

Expand|Select|Wrap|Line Numbers
  1. for(int x = 0 ; x < spaceLine.length() ; x++){
  2.         cout <<spaceLine[x]<<endl; 
  3.         tempVec.push_back(spaceLine.substr(x, x+1));
  4.     }

there are spaces of different size.... any suggestion to fix it?!?

you dont see it in the output but quotes are of different sizes...

also thanks for fixing the error!
Nov 21 '08 #6
boxfish
469 Recognized Expert Contributor
I am using this code in the main function,
Expand|Select|Wrap|Line Numbers
  1.     spaceLine.insert(0, noofspaces, ' ');
  2.     cout << "Spaces Line:" << spaceLine <<"endl"<<endl;
  3.  
  4.     for(int i = 0 ; i < tempVec.size(); i++){
  5.         cout << "\"" << tempVec[i] << "\"" << endl;
  6.     }
  7.     addToMap(tempVec,mymap);
  8.     for(int x = 0 ; x < spaceLine.length() ; x++){ 
  9.         cout << "\"" << spaceLine[x] << "\"" << endl;  
  10.         tempVec.push_back(spaceLine.substr(x, x+1)); 
  11.     }
  12.  
and I am not getting spaces of different widths.
So can you please post some more of the modifications you have made?
Nov 22 '08 #7
drjay1627
19 New Member
@boxfish
I figured it out! But couldn't have done it without your help mate!

instead of => tempVec.push_ba ck(spaceLine.su bstr(x, x+1));
I did => tempVec.push_ba ck(spaceLine.su bstr(x, 1));

When its x+1 the first substring will be an empty string with size 1 an second will be empty string with size 2 and so on... Because we loop through the string.

tempVec.push_ba ck(spaceLine.su bstr(x, 1)); does what I want it to do. I didn't think about it much after that, because I ran into another problem. I wanted to post this early but bytes was down for a long time on Saturday.

Again thanks for you help!

drjay
Nov 24 '08 #8
boxfish
469 Recognized Expert Contributor
Oh, sorry, I see I was wrong about that. Argument 2 is the size of the substring. I'm glad you got it working though, and I'm glad I was of some help.
Nov 24 '08 #9

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

Similar topics

8
3196
by: Mark | last post by:
My PHP script builds a table that is too wide to fit on the paper. Two of the columns contain strings that are more lengthy than data in the other columns. I can get the table to fit by letting the browser break the lengthy strings across two or more lines, thus allowing the column to get skinnier. I do this by inserting spaces in various places in the string. But this is not a desirable solution as it would mislead the user into...
2
1693
by: Papajo | last post by:
Is there a script that would add a space in front of and behind any text adding to a form box, it coud be triggered by either an onClick or onBlur event handler. Any help s appreciated. Thanks Joe
8
22590
by: VM | last post by:
I'd like to add several spaces to a string variable. For example, if the value is "VM", I'd like to add 7 spaces so the new value is "VM ". Is that possible? Thanks.
135
7553
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
4
1676
by: Duncan Dimech | last post by:
Dear All I am writing a tool which requires to have controls added to it dynamically. To make the task more complex, the addition of the control cannot happen anywhere but it has to be instead of a token example <pthis is a test <textbox /and a text box should be entered </P> the tool should render the message and instead of <textbox /it should
1
2202
by: MaRkHaSBEEnMade | last post by:
Hi there I currently have a listbox that displays provinces,regions and suburbs.I want to pad the items logically with spaces,but the spaces are being igonred... eg Province1 Regions1 region2 Province2 Region3
15
15630
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another output string. But this was trivial. The other option is to use pointers and shift all the characters after the space by one space to the left. I did this program using pointers and then using array too and I get segmentation fault. What is going...
2
1564
by: psbasha | last post by:
Hi I would like to edit the string in a give line of string. Sample >>> str1 = 'Pnt1 1000 1 2 3 ' >>> str2 = str1 >>> str3 = '2000 ' >>> str4 = str2 + str3 + str1 >>> str4
1
1911
Ajm113
by: Ajm113 | last post by:
For some reason when I try to pass a string that is this: 'man!'; Into the first param of copy and the value of 4 in to the second param and 6 into the last argument for some reason copy is adding duplicate characters in the string. Then it comes out like so: man!!'; It's kinda odd because if I form the string into a sentence it works fine, because of the spaces can someone help me out why it's doing this? I just want it to get the...
0
10551
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.