C++ :: C Function To Sort A String Based On Delimiter
Apr 7, 2012
I Need to write a function using C wherein I should do the following:
(i) The function will receive a string in a character pointer
(ii) This string will adhere to the following structure:
"Kentucky+New York+Arizona+Nevada"
The number of states can differ from 4 to 50
The delimiter between States can differ from '+' to ',', hence I would like to pass the delimiter to the function.
(iii) This string should then be sorted alphabetically from left to right.
The above example would then become: "Arizona+Kentucky+Nevada+New York"
(iv) This string needs to be returned from the function using a character pointer.
View 3 Replies
ADVERTISEMENT
Mar 19, 2014
How delimiter work. I need write a function that splits the string by delimiter. Function header is:
vector<string> split(stirng target, string delimiter);
for example, the code inside main calling your function
vector<string> v = split("hiAAAmyAAAnameAAAis", "AAA");
and v should come out as hi my name is
So is it something like
vector<string> split(string target, string delimiter) {
vector<string> word;
string s = "hiAAAmyAAAnameAAAis";
string delimiter = "AAA";
View 9 Replies
View Related
Feb 4, 2014
How do I use a string in place of a character in the function getline?
For example,
getline(infile,lines,'}');
This works fine, but I want the delimiter of } to be "};", but I can't do this as it only takes in characters, not strings.
I want:
getline(infile,lines,"};");
Any way to get around this?
View 1 Replies
View Related
Mar 3, 2014
Write one program that receive a string, tokenize it by ' ' delimiter and show the output, using STL
So, I write:
#include <stack>
#include <iostream>
#include <vector>
#include <string>
using std::stack;
using std::cout;
[Code] .....
And...
Type any string and I'll say its proprieties:
> A B C
String length: 5
Words number: 3
Word 1: (split(input)[0])
A
Word 2: (split(input)[1])
B C
Word 3: (split(input)[2])
C
>
What am I doing wrong?
View 3 Replies
View Related
Jun 27, 2013
I've got some functions and macros that I want to execute based on a string input that matches the function's name. I came across as this being a possible solution that I'd like to pursue IF it is possible to do. To clarify, I want to be able to look at a string and if the string matches the name of a defined function or macro then it will execute. Is there an effective way to do this (or is it even possible)?
It has to be pretty robust and dynamic given the project's purpose. Basically there's an input file that is being parsed and functions should execute if called upon in the file. I can't simply match strings to their corresponding functions in decision statements, as this isn't robust and isn't scalable.
View 3 Replies
View Related
Jan 19, 2014
i am currently using a comparator function in STL map for sorting date of string data type in ascending value. My dates are in this format for e.g. 15OCT1990, 13SEP1980 and etc. I am using substring to split up the string so that i can compare day, month, year separately. Right now i have problem comparing the month portion because alphabetically "FEB" comes before "JAN".How can I make a fixed substring position of (2,3) which is my month value to accept that string value of JAN comes before FEB,AUG,DEC for e.g?
struct sortMapDaily: public std::binary_function <bool, std::string, std::string>
{
bool operator() (const std::string& lhs, const std::string& rhs)
{
[Code].....
View 5 Replies
View Related
Apr 22, 2013
I can't sort node based upon there value to sort nodes on the basis of the Nodes value in ascending order
if(Current_Node->get_value() < Last_Current_Node->get_value())
{
temp = Current_Node->get_next();
Current_Node->set_next(Last_Current_Node);
Last_Current_Node->set_next(temp);}
View 3 Replies
View Related
Apr 12, 2014
I'm attempting to build a column based database, and I'm new to C++ (just wanted to play around with building a column base database "for the fun of it"). I want to construct a very fast radix sort, that would allow me to quickly sort groups of columns based on integer values. My general preference is to take up more RAM to get more performance.
I'd like to build the radix sort by allowing 256 "buckets" to drop values in as I'm sorting. This allows me to sort through a group of 4 byte integers in only 4 passes. But assuming I'm trying to sort a large group of values (say 50+ million), I'm not sure what type of container to use for these. Also note I'm pretty unfamiliar with the "standard library", so below are my thoughts:
Vectors:
-Pros: Easy to use, and very fast for sequential and random access inserts / reads
-Cons: If they have to dynamically resize because a given vector wasn't large enough, this can apparently really slow performance. Unless I make another pass over the numbers before I start sorting, I wouldn't know how big to make individual the individual vectors. This means I either have to make them "too big" and waste space, or pay a performance price for either resizing, or scanning data first.
Lists:
-Pros: Seems like I wouldn't have to specify size ahead of time, so I could just easily insert values to a given list. Also, since I don't need random access reads (I'll ready the "0" list sequentially, then the "1" list, etc. they should work fine.
-Cons: I don't really know much about lists, but I'm not sure how easy it is to append a new value to the end of a list. I've read that standard library lists include both "forward" and "backward" pointers, which I don't need. Also, I find it hard to believe that there isn't some time taken up with memory allocation. If I build a list and append x million records in it, is it calling memory allocation routines x million times?
Or maybe there's another container type I should learn?
Again, my goal is to make this "fast", not "memory efficient". But having said that, the fastest way I could think of (use 256 vectors, each sized equal to the total number of members to be sorted) is just too much memory to burn - 256 times a vector big enough to hold millions of elements is too much.
View 4 Replies
View Related
Aug 19, 2013
Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.
Code:
typedef unsigned long long UI64;
typedef unsigned long long *PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) {
size_t mIndex[8][256] = {0};
/* index matrix */
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;
[Code]....
View 9 Replies
View Related
Mar 22, 2013
how to sort a 1D array using function sort().but if i have a 2D array how do i sort only 1 row of it (or column)?
View 2 Replies
View Related
Apr 23, 2013
I want to convert the integer into a string.
int x = 693;
char chararr[max];
In my homework, x is unknown. but don't worry, I wont ask for the full code. I just need the part where you change the int into a string/array of char.
I'm thinking in circles from where to start?
View 2 Replies
View Related
Apr 23, 2013
Generate a binary ascii characters(weird character ) based on Hex:54313032202020303030 data in C++
View 3 Replies
View Related
Apr 1, 2014
I have a problem I am working on where I need to sort some data based on the values of a string of bits. The strings look like this,
010000001110000000
there are 18 bits, 1 means a feature is present, 0 means the feature is absent.
Each of these string has 4 on bits. I need to sort them such that I have the longest possible runs with 3 of the same on bits. It doesn't matter which 3 bits are on, I am just looking to order them in blocks with the longest possible runs. As a second step, the ordered blocks will be sorted by size large>small.
The following data is ordered like I need it to be.
Code:
// block 1, run of 12, keys 1,2,11 are identical (key 12 is also identical)
011000000001100000
011000000001100000
011000000001100000
011000000001100000
[Code] .....
This is the sort order that I am looking for. I need to be able to take a list of the bit strings in any particular order and sort them into the order above. The algorithm would need to recognize that there are 4 on keys and then look for groupings of three common on keys.
This is more of an algorithm question than one about specific implementation in code. I generally assume that most programming problems have been solved one way or another, so I don't know much about analyzing and manipulating strings of bits.
Is there a standard method for this kind of pattern recognition?
View 14 Replies
View Related
Jul 6, 2014
error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)
Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();
[Code] .....
View 11 Replies
View Related
May 9, 2014
I have a homework assignment in C++ where I have to: "Write a program which asks user to input 10 students names. Store them in an array. Convert all the names to UPPERCASE without using any built-in functions. You must write the function to do that yourself. Lastly, sort the name in alphabetic order."
Here is my code so far, but my program won't compile, and the message it gives me is gibberish.
#include <iostream>
using namespace std;
void sortNames(string name[], int cap);
void toUpper(string name[]);
int minIndex(string name[], int i);
void sort(string name[]);
void swap(string name[], int i, int j);
[Code] .....
View 3 Replies
View Related
Dec 21, 2014
Is it possible to fgets() the string (or word) only after a delimiter? I yes then how?
Example: Code: printer, scanner, machine
Also, how can I sscanf() a string with an indefinite number of sizes and assign it to only one variable?
Example:
Code:
str = "I Love C programming 9000";
sscanf(str, "%s %d", strvar, intvar);
View 13 Replies
View Related
Mar 26, 2014
Some background: I have a class, A, with members, B and C and D; I also have an array of A objects; I want to be able to have a function which takes said array and performs a certain calculation on either the B, C, or D members of each of the A objects, depending upon certain circumstances; I want to perform the same calculation regardless of which member is to be used in said calculation, such as always assigning the value 3 or multiplying the member's value by a cofactor of some sort.
My question, therefore, is: how I might do this using only one function be it a template or not?
View 1 Replies
View Related
Feb 2, 2013
i have a list of date format 1-12-2011 that i get from a txt file.
char date[30];
fstream fin("date.txt");
fin >> date;
how do i split the date array to 3 array of char day[],char month[] and char year[] for my structure list? using delimiter '-' so i get 1 to day, 12 to month and 2011 to year.
struct date{
string day;
string month;
string year;
}
View 2 Replies
View Related
Feb 23, 2014
I have attached the file that I need to read into a data structure. In the example I am just printing it to the screen. This following code has worked for me before, but now isn't. I have tried in visual studios, and on unix, and neither are running it. I ultimately need it to run on unix.
Example file:
word
book
programming
Here is my function to read in the file.
ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(!file.eof()){
getline(file, line);
cout << line <<endl;
}
}
file.close();
View 7 Replies
View Related
Oct 18, 2014
This triangle is different from all other triangles in this way that it prints words separated by spaces. The output should look like:
this
this is
this is the
this is the best
this is the best way to
this is the best way to spend
this is the best way to spend time
this is the best way to spend time for
this is the best way to spend time for reedaf
so far i have a code that prints
this
is the
best way to
spend time for reedaf
The code is :
#include <stdio.h>
int main() {
char msg[]="this is the best way to spend time for reedaf";
int inn=1, out, i=0, max;
max=(sizeof(msg)/sizeof(int))+1;
char *output;
[Code] .....
How to start every line with the first word in the array. How to print the starting word and then the next word and then go to the next line. How to print again the starting word and then the next word and then the next and then goto the next line so on and so forth.
View 4 Replies
View Related
Oct 3, 2013
I want to read a binary file using as line separator "ff77" in order to parse further each line one by one with some regex since the file is big. I have a small ruby code shown below, but I'm new in C++, and I don't know how to replicate in C++ what this ruby code does.
Code:
#!/usr/bin/env ruby
BEGIN{ $/="xffx77" } # Line separator = FF77
File.open(ARGV[0],"rb") # Open in binary mode
[Code].....
View 14 Replies
View Related
Nov 2, 2013
I run with debugger and appear this file referring the error to line 142 (in red):
Code:
/***
*xtoa.c - convert integers/longs to ASCII string
*
* The module has code to convert integers/longs to ASCII strings. See
*
*******************************************************************************/
#include <cruntime.h>
#include <stdlib.h>
#include <limits.h>
#include <tchar.h>
#include <internal.h>
#include <internal_securecrt.h>
#ifdef _UNICODE
#define xtox_s xtow_s
[Code] ...
In the calls stack window appears this
Code:
=>msvcr110d.dll!xtoa_s(unsigned long val, char * buf, unsigned int sizeInTChars, unsigned int radix, int is_neg) Line 142C
msvcr110d.dll!_itoa_s(int val, char * buf, unsigned int sizeInTChars, int radix) Line 176C
Get_Blocks.exe!main(int argc, char * * argv) Line 224C++
Get_Blocks.exe!__tmainCRTStartup() Line 536C
Get_Blocks.exe!mainCRTStartup() Line 377C
kernel32.dll!7695336a()Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!76f19f72()Unknown
ntdll.dll!76f19f45()Unknown
It seems could be becuase to _itoa_s(), I'm using like this:
Code:
_itoa_s(CONVDEC(i), num, 10, 10);
sub += num;
View 5 Replies
View Related
Mar 13, 2014
I'm making a simple tile-based RPG and I've run into a bit of trouble when I put my code into classes.
Heres my main
#include <iostream>
#include "windows.h"
#include "math.h"
#include "time.h"
#include <string>
#include "Goblin.h"
#include "User.h"
#include "Inventory.h"
#include "Maps.h"
using namespace std;
void treasureGet();
[Code] ....
My other 3 classes have the same structure and the same error for all their functions.
This is the exact Error
undefined reference to `Goblin::goblinBattle()'|
undefined reference to `Goblin::goblinBattle()'|
undefined reference to `Goblin::goblinBattle()'|
undefined reference to `Goblin::goblinBattle()'|
undefined reference to `Inventory::inventoryOpen()'|
undefined reference to `Goblin::Goblin()'|
undefined reference to `User::User()'|
undefined reference to `Inventory::Inventory()'|
undefined reference to `Maps::Maps()'|
||=== Build finished: 9 errors, 29 warnings (0 minutes, 0 seconds) ===|
View 4 Replies
View Related
Dec 5, 2014
I am trying to read a file use the data line by line to create into an object. The current file I have is like this and the code reading the file will be found below.
1223 Fake1 Name1 60 70 80 24 89 add1 Male
1224 Fake2 Name2 61 70 81 80 24 add2 Male
1225 Fake3 Name3 63 70 82 80 89 add3 Male
1226 Fake4 Name4 63 70 83 80 88 add4 Male
The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.
I would like to change my file to this;
1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male
1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male
1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male
1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male
How can I update the code below so that it can use the delimiters to create an object?
void loadFile(Person people[], int* i) {
ifstream infile("people2.txt");
if ( !infile.is_open()) {
// The file could not be opened
cout << "Error";
[Code] .....
View 5 Replies
View Related
Nov 20, 2014
I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.
To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):
// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;
The two example functions I want to use are defined in that struct ("Data"):
// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}
The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:
// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}
The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);
the error becomes: "expression must have a (pointer-to-)function type."
The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:
// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);
This bit shows no errors by itself. So, my questions:
a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.
b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing
// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object
but that doesn't work ("a pointer to a bound function may only be used to call the function").
View 4 Replies
View Related
Feb 28, 2013
Im suppose to make a "poor mans" variation to the Sort function built into unix. The program is suppose to read in a file and sort the contents of the file. So its a variation of the Unix Sort feature. I have remade the readLine function we were provided so that it doesnt use fgets. where to go from here, Not sure on how to make a sort function. Here are the reqirements of the program:
Code:
• Re-implement the readLine() function so that it no longer makes use of fgets(). Instead,
process the input on a given line character-by-character.
• Provide code which will create a data structure similar to argv to hold all of the words to be sorted. Use malloc() to ensure that each entry has just the required number of bytes needed to store the words. The final entry in your array should be the NULL pointer.
• Implement a sort() function which will rearrange the words in sorted order. To swap two words in your array, note that only a pair of pointers need to move. The strings themselves, once established, will never move. Heres what i have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINES 1000 /* maximum number of reminders */
#define WORD_LENGTH 10 /* max length of reminder message */
[code]....
View 9 Replies
View Related