473,385 Members | 1,973 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,385 software developers and data experts.

adding # of spaces in string to map

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.insert(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::string, 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_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<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 parsingForPunctuations(string line);
int parsingForSpaces(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 = parsingForPunctuations(str);
int noofspaces = parsingForSpaces(str);
cout << "str: " <<str<<endl;
//cout << "newstr: " <<newstr<<endl;
cout << "# of spaces: " <<noofspaces<<endl;

string spaceLine;
vector<string> tempVec;

tempVec.push_back("hello");
tempVec.push_back("world");
tempVec.push_back("apple");
tempVec.push_back("cat");
tempVec.push_back("hello");
tempVec.push_back("world");
tempVec.push_back(" ");
tempVec.push_back(" ");

spaceLine.insert(0, noofspaces, ' ');
cout << "Spaces Line:" <<spaceLine<<"endl"<<endl;

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

return 0;
}

string parsingForPunctuations(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 parsingForSpaces(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>(word[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 4712
boxfish
469 Expert 256MB
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
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 Expert 2GB
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
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
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 Expert 256MB
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
@boxfish
I figured it out! But couldn't have done it without your help mate!

instead of => tempVec.push_back(spaceLine.substr(x, x+1));
I did => tempVec.push_back(spaceLine.substr(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_back(spaceLine.substr(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 Expert 256MB
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
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...
2
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
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
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...
4
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...
1
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 ...
15
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...
2
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 +...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.