string parsing question

if i have a string like follows :

"33,33,33,1024,561,21,21"

I want to do two things:
1- extract the values between the last "33" and first "21" .
2- convert the extracted values to an integer.

so from above string i want to be able to extract numbers 1024,561

note: I want to put these extracted numbers into seperate variables.


Last edited on
Are your delimiters always 33 and 21?

If so finding the last "33," and the first ",21" using one of the std::string.find functions and a stringstream might b of some help.

Otherwise you'll need to clarify the problem.
hi Jib as you know from my other post "http://www.cplusplus.com/forum/general/182983/" this would be a C solution and not a C++.
yes my delimiters are always going to be 33 and 21.
but how can i construct the string from an array on first place using C commands?
so i basically have two step operation, first convert the array of chars into a string and then extract the numbers based on the criteria and save them into variables or int array
hi Jib as you know from my other post

No, I didn't realize at the time that these two topics were the same. This a good reason to stop opening new topics when you have an existing topic already open. I suggest you stick with the other topic.

so i basically have two step operation, first convert the array of chars into a string

You said in the first post of this topic that you already have a string:
if i have a string like follows :

In C a string is an array of char terminated by the end of string character ('\0').

this post was on how to parse the string and the other post was part of the step on how to get the string in first place thats why i opened two different posts.
can we close the other one since the issue is now resolved , i couldnt use string since its c++ feature and not available in AVRGCC?
note: i have marked the other post as resolved .
Last edited on
This should get you started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <sstream>
#include <iostream>
#include <locale>

struct custom_delimiter : std::ctype<char>
{
    custom_delimiter() : std::ctype<char>(get_table()) {}
    static mask const* get_table()
    {
        static mask rc[table_size];
        rc[','] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space;
        return rc;
    }
};

int main()
{
    const std::string data { "33,33,33,1024,56,21,21" };
    std::istringstream is(data);
    is.imbue(std::locale(is.getloc(), new custom_delimiter));

    std::string line;
    while (is)
    {
        is >> line;
        std::cout << line << std::endl;
    }
}
This should get you started.

I doubt that very much. The compiler in use doesn't have most of the C++ features available, such as std::string, and anything else that uses either templates or exceptions. So this will probably require a C solution.

I recommend using sscanf() to parse the string. Another option would be strtok() and atoi().
Is he using one of those pre 98 compilers, Borland perhaps? Here's the equivalent using strtok() to parse the string.

These two versions, parse that string for you. You need to apply your own logic to complete your assignment. Take some time to atleast understand this version of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//#include <iostream.h> // for old pre-standard compilers

#include <iostream> // standard alternative
using namespace std;

#include <string.h>

int main()
{
    char data[] = "33,33,33,1024,56,21,21";
    const char custom_delimiter[] = ",\n";

    if (char* line = strtok(data, custom_delimiter))
    {
        cout <<  line << std::endl;

        while ((line = strtok(NULL, custom_delimiter)))
        {
            cout <<  line << endl;
        }
    }
}
Last edited on
no I am using Atmelstudio's AVRGCC compiler .
iostream does not exist in AVR GCC aparently , any other way ?
The only thing the iostream library is doing is printing to the console, since I doubt that your system has a console just remove the #include iostream line and the #include namespace std; line and replace the cout lines with whatever logic you need for your program. The while loop is separating the different numbers in your string to individual strings that you can use in your program.

The stream is not the point. It's just used to print the parsed value. I was hoping to demonstrate how to parse that string; I thought that was the problem.

I'm not going to write the whole program for you.
Topic archived. No new replies allowed.