2175 Shares

C++ How do I split a char string into substrings?

C++ How do I split a char string into substrings? Topic: Case string
July 18, 2019 / By Hulda
Question: I want to tokanize a char string into substrings that i will then store with different variables. Here's what i have... int i = 0, j = 0; do { if(isalpha(str[i]) && j == 0 ) { strncpy(substring1, str,i+1); } if(isspace(str[i])) { j++; } else if(isalpha(str[i]) && j>0) { substring2 += str[i]; } i++; } while(i <= strlen(str)+1); I know this is not close at all to a good algorithm, and for right now I can only correctly display str and substring1. I don't know how to store middle and end parts of the cstring. Please help!!!!
Best Answer

Best Answers: C++ How do I split a char string into substrings?

Edweena Edweena | 1 day ago
Here's a quick solution I came up with, the main implementation is the "tokenize" function. I wasn't quite sure of your explanation of the task, but I'm guessing you want a way to split a string into a number of sub strings based on (in this case) the space character? http://pastebin.com/fiz7nGFv I commented it so you can learn :)
👍 110 | 👎 1
Did you like the answer? C++ How do I split a char string into substrings? Share with your friends

We found more questions related to the topic: Case string


Edweena Originally Answered: How to split a string in bat file?
Ummm... The bad news is I don't think you can directly. The good news is that you probably can write a short program that can be called that will do this for you in C or C++ with little or no trouble. Sorry, but that's the best I remember. Try the free compilers and tutorials at the URL below to create the program.
Edweena Originally Answered: How to split a string in bat file?
worldwide The_Array() As variation Sub load_arrays() Const myFile = "C:records.txt" Dim data_line As String Dim i As Integer i = a million Open myFile For enter As #a million Do till EOF(a million) Line enter #a million, data_line 'develop length of array to handle new get entry to ReDim safeguard The_Array(3, i) The_Array(a million, i) = Trim(Mid(data_line, a million, 3)) The_Array(2, i) = Trim(Mid(data_line, 4, 3)) The_Array(3, i) = Trim(Mid(data_line, 7, 3)) i = i + a million Loop close #a million end Sub functionality getZ(x As variation, y As variation) Dim i As Integer For i = a million To UBound(The_Array, 2) If The_Array(a million, i) = x And The_Array(2, i) = y Then getZ = The_Array(3, i) go out functionality end If next i end functionality

Charleen Charleen
you will possibly be able to apply .ToCharArray() on any string to get an array of char. in case you may prefer to teach any char to a string, use .ToString() on the char fee. char[] textual content fabric = "hi international".ToCharArray(); foreach(char c in textual content fabric) { Console.WriteLine(c.ToString()); } savor!
👍 40 | 👎 -8

Charleen Originally Answered: How to use a char* that has no name in a function with format: Function(const char* ); in c++?
You don't, at least not if the name is omitted from the function header. Leaving the parameter name out says "The caller is going to pass me an argument. That's legal and expected, but I don't intend to use its value." This is not the same as leaving the parameter name out of the prototype. Parameter names are always optional in prototypes. If provided, they don't have to match the names used in the function header. Example: int add(int, int); // prototype for a funciton that adds two ints int add(int a, int b) { .... return a+b; } That's legal in C or C++. The function body uses the argument names as declared in the function header "int add(int a, int b)" and ignores the prototype. (In C there will be a check to see that the arguments and return type are the same. C++ will allow a difference but will check for valid overloads.) You could have used entirely different names for the prototype arguments and it would not matter. Ex: int add(int x, int y); // still valid for the add function above All that said, I think it's poor style to omit names in prototypes. If nothing else, they are handy reminders of what the arguments are expected to be.
Charleen Originally Answered: How to use a char* that has no name in a function with format: Function(const char* ); in c++?
If you are using the function prototype then it does not have to have a name to be useable. eg Function("this is a test"); OR char words[] = "this is a test"; Function(words); etc. If you have to write the function yourself you can make up any name you want:- void Function(const char* str){ // do stuff with variable named str }

If you have your own answer to the question case string, then you can write your own version, using the form below for an extended answer.