Zeke Rich
asked on
How to split a string in C/C++ ?
I wrote a function to update the time of a RTC (real time clock).
The function is receiving a time variable: 18:21 called String x
To update the rtc i need to split this string up into 2 variables; hour and minutes so i can update the current time.
Im having some trouble splitting the string, its not working. Could you help me with this code? Thanks for your help!
::UPDATE::::
Im sorry i left out this critical information... this is being programmed on a The Wemos D1 Mini that is roughly the size of a quarter. It doesnt have the C++ Standard library and Standard Template Library and only has 64 KB of SRAM, 4MB of flash memory, 80MHz of system clock, around 50k of usable RAM and an on chip Wi-fi Transceiver.
Link: https://docs.zerynth.com/latest/official/board.zerynth.wemos_d1_mini/docs/index.html
The function is receiving a time variable: 18:21 called String x
To update the rtc i need to split this string up into 2 variables; hour and minutes so i can update the current time.
Im having some trouble splitting the string, its not working. Could you help me with this code? Thanks for your help!
::UPDATE::::
Im sorry i left out this critical information... this is being programmed on a The Wemos D1 Mini that is roughly the size of a quarter. It doesnt have the C++ Standard library and Standard Template Library and only has 64 KB of SRAM, 4MB of flash memory, 80MHz of system clock, around 50k of usable RAM and an on chip Wi-fi Transceiver.
Link: https://docs.zerynth.com/latest/official/board.zerynth.wemos_d1_mini/docs/index.html
void updateTime(){
String x = server.arg("var1"); // gets the time 18:21
String z = server.arg("var2"); // not used yet
// get input time
String time = strtok(x, ":"); // trying to split this here
String hour = time[0];
//if(hour == '00') {hour = 24}
String min = time[1];
String result = x + "<p> Time Updated</p>";
rtc.adjust(DateTime( , , , hour, min, )); // this will update the rtc like this: rtc.adjust(DateTime(y, mon, d, h, minu, s));
server.send(200, "text/plane",result);
}
@Zeke Rich
Base version 0.0
Base version 0.0
// I have written only sample code.
#include <string>
#include <iostream>
using namespace std;
void updateTime();
class TIME
{
public:
string arg(string var1)
{
return "18:21";
}
void send(int,string,string)
{
}
}server;
void updateTime()
{
string x = server.arg("var1"); // gets the time 18:21
string z = server.arg("var2"); // not used yet
// get input time
cout << "VALUE OF x[ " << x << " ]\n";
string hour = strtok((char*)x.c_str(), ":"); // trying to split this here
// If not required you can save x to other string and use that string as base version for strtok.
cout << "After strtok1 VALUE OF x[ " << x << " ]\n";
cout << "VALUE OF hour[ " << hour << " ]\n";
string min = strtok((char*)x.c_str(), ":"); // trying to split this here
cout << "After strtok2 VALUE OF x[ " << x << " ]\n";
cout << "VALUE OF min[ " << min << " ]\n";
cout << "VALUE OF x[ " << x << " ]\n";
string result = x + "<p> Time Updated</p>";
// rtc.adjust(DateTime( , , , hour, minute, )); // this will update the rtc like this: rtc.adjust(DateTime(y, mon, d, h, minu, s));
server.send(200, "text/plane",result);
cout << "hour: " << hour << " min: " << min << "\n";
return;
}
int main()
{
updateTime();
return 0;
}
/*
$ /usr/bin/g++ -Wall ./29174496.cpp -o ./a.out
$ ./a.out
VALUE OF x[ 18:21 ]
After strtok1 VALUE OF x[ 1821 ]
VALUE OF hour[ 18 ]
After strtok2 VALUE OF x[ 1821 ]
VALUE OF min[ 18 ]
VALUE OF x[ 1821 ]
hour: 18 min: 18
$
*/
Hi,
Let's leave C-Style strings to where it belong (AKA C) and focus on C++.
There are numerous way to split a string (google will return a bunch of results), like:
Using boost::split().
Documentation: https://www.boost.org/doc/libs/1_68_0/doc/html/boost/algorithm/split.html
Sample code: https://www.geeksforgeeks.org/boostsplit-c-library/
Or write a function:
Let's leave C-Style strings to where it belong (AKA C) and focus on C++.
There are numerous way to split a string (google will return a bunch of results), like:
Using boost::split().
Documentation: https://www.boost.org/doc/libs/1_68_0/doc/html/boost/algorithm/split.html
Sample code: https://www.geeksforgeeks.org/boostsplit-c-library/
Or write a function:
#include <vector>
#include <sstream>
std::vector<std::string> split(std::string const& input, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream{ input };
while(std::getline(tokenStream, token, delimiter))
{
if(token != std::string())
tokens.push_back(token);
}
return tokens;
}
ASKER
::UPDATE::::
Im terribly sorry i left out this critical information... this is being programmed on a The Wemos D1 Mini that is roughly the size of a quarter. It doesnt have the C++ Standard library and Standard Template Library and only has 64 KB of SRAM, 4MB of flash memory, 80MHz of system clock, around 50k of usable RAM and an on chip Wi-fi Transceiver. So i wouldnt be able to use vector classes and can only use small arrays as large as the board can hold. It is being programmed with the Arduino IDE.
Link: https://docs.zerynth.com/latest/official/board.zerynth.wemos_d1_mini/docs/index.html
Im new to C++ and programming and didnt realize how complex C++ could be. Thank you for teaching me.
Im terribly sorry i left out this critical information... this is being programmed on a The Wemos D1 Mini that is roughly the size of a quarter. It doesnt have the C++ Standard library and Standard Template Library and only has 64 KB of SRAM, 4MB of flash memory, 80MHz of system clock, around 50k of usable RAM and an on chip Wi-fi Transceiver. So i wouldnt be able to use vector classes and can only use small arrays as large as the board can hold. It is being programmed with the Arduino IDE.
Link: https://docs.zerynth.com/latest/official/board.zerynth.wemos_d1_mini/docs/index.html
Im new to C++ and programming and didnt realize how complex C++ could be. Thank you for teaching me.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you experts, its my fault for not leaving enough details to solve the problem.
However all your answers were incredible and i learned a lot.
Special thanks to sarabande who gave me the code I ended up using.
However all your answers were incredible and i learned a lot.
Special thanks to sarabande who gave me the code I ended up using.
https://fresh2refresh.com/c-programming/c-strings/c-strtok-function/
Please note the use of char array, and how the results are retrieved after strtok()