void split_to_array(string sentence, string splitter)
{
int index = 0;
int last_index = sentence.find(splitter);
int array_amount = 0;
for (int i = 0; i < sentence.size(); i ) {
cout << sentence[i] << endl;
if (sentence[i] == splitter) array_amount ;
}
}
int main()
{
split_to_array("fdsfds fdsfds vcxvcx fdsafdsa", " ");
}
I am trying to get the length of the array by finding the splitter argument in the string, However when I try to compare the index of the string with the splitter I get an error that I cannot compare a char with a string which is the first issue I have. When I try to turn it into a string by using std::to_string I get a number back instead of the actual string.
CodePudding user response:
You cannot do this
if(sentence[i] == splitter)
sentence[i]
is a single character. splitter is a string.
I would fix it by passing a char argement for splitter. ie
void split_to_array(string sentence, char splitter)
and
split_to_array("fdsfds fdsfds vcxvcx fdsafdsa", ' ');
if you want to keep the same function signatures for some reasons then do
if(sentence[i] == splitter[0])
ie compare against the first (and only) characer in splitter. This is however a very odd thing to do.
CodePudding user response:
void split_to_array(string sentence, char splitter)
{
int index = 0;
int last_index = sentence.find(splitter);
int array_amount = 0;
for (int i = 0; i < sentence.size(); i ) {
cout << sentence[i] << endl;
if (sentence[i] == splitter) array_amount ;
}
}
int main()
{
split_to_array("fdsfds fdsfds vcxvcx fdsafdsa", ' ');
}
Maybe do it like this. Eventually you send "splitter" parameter as a char so it is easier to compare. You need to use apostrophe (') for a char when calling a function.
CodePudding user response:
You need something like this (Can be also done in C) :
// This is called a prototype to declare a function or a symbol without restricting it to be at the top
// It's like an uninitialized "int abc;" which will be set later
// First, you need to understand pointers and memory address, and the better way is to practice C language rather than using the libraries, this will give you a big advantage in programming
// Also you must understand how compiling and linking works, and how executable images work
int main(){
char* a = "Hello World" // The content is the address of the string "Hello World", but not hello world itself
SplitString(a, " "); // consider not using ' ' because it becomes a char not char* (pointer)
// lets say hello world is in address 0x1000
// 0x1000 - ['H' - 'e' - 'l' - ... 'l' - 'd' - '\0'] (\0 specifies the end of the string)
// for ex: incrementing char* will make it's value 0x1001 which will prints "ello World" and thats how it works
}
void SplitString(char* str, char* SplitBy){
// First char* is a memory address reference to a chain of characters (bytes containing ASCII code of characters)
// Get the length of the split string
size_t SplitLen = strlen(SplitBy);
int ArrayLength = 0;
// Get the last pointer of the string we wan't to cut off
char* last = str;
// we are using this to calculate the size of the splitted string
int size = 0;
while(*str /*while the value stored in str is not zero, increments str which is a memory address*/){
// compare content of current strings pointed by str and SplitBy
if(memcmp(str, SplitBy, SplitLen){
// If they are equal then "char* last" contains the address of the last string to print it's value
for(int i = 0;i<size;i ){
cout >> last[i];
}
cout >> endl;
// increments last to the new string address
last =size SplitLen;
// resets string size
size = 0;
ArrayLength ;
} else
size ; // increments string size
}
// prints content of the last string
cout >> last >> endl;
}