#C++ How do I Split a C string into 2 variables

1 messages · Page 1 of 1 (latest)

remote stratus
#

Right now I'm trying to figure out how to split a cstring by a space via a loop. I'm not exactly sure on how to do it.
This is a really rough example of what I'm trying to do rn (I know this isn't a full fuctional code this is just a really rough example of what I'm doing or trying to do)

struct()
{
char first[15] // Issue is trying to split the first variable at the space to set it into this variable
char second[15] // then trying to to set the 2nd word.
}
int main()
{
char fullname[15] = "John Doe";
}

I'm trying to initialize the two words into the structure by splitting the name can anyone give me a explaination on how I would do that?

glacial sorrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

remote stratus
#

C++ How do I Split a C string into 2 variables

bold pawn
#

;compile

#include <string>
#include <stdexcept>
#include <iostream>

struct Person
{
    Person(const std::string &fullname)
    {
        const auto space_pos = fullname.find(' ');
        if (space_pos == std::string::npos)
        {
            throw std::runtime_error("Fullname with no space!");
        }
        
        firstname = fullname.substr(0, space_pos);
        surname = fullname.substr(space_pos + 1);
    }

    std::string firstname;
    std::string surname;
};

int main()
{
    const Person p("John Doe");
    std::cout << "First name = " << p.firstname << '\n';
    std::cout << "Surname = " << p.surname << '\n';
}
fierce graniteBOT
#
Program Output
First name = John
Surname = Doe
remote stratus
#

I'm like looking at this and like don't really understand much of it

bold pawn
#

Okay lets go bit by bit. Do you understand all of main() ?

remote stratus
#

Main yeah I understand that alright

#

Is within the structure where is like kinda confusing me

#

I think mainly because what you put is something I haven't learned yet

bold pawn
#

Are you familiar with std::string?

remote stratus
#

yes

bold pawn
#

Okay so a Person is just a struct with two data members firstname and surname

#

The function in the struct Person called Person(const std::string& fullname) is a special type of function called a constructor. The purpose of a constructor is to 'construct' a type.

#

It's job is basically to make sure that the struct starts off in a good, valid state

#

With me so far?

remote stratus
#

yeah I'd say the part that throws me off the most is like runtime_error and substr

bold pawn
#

Ok I'll go through the constructor line by line

remote stratus
#

k

bold pawn
#
const auto space_pos = fullname.find(' ');

fullname is a std::string. std::string has a function you can call called find() who takes in a character or string and returns the index of its position

#

Im just setting space_pos to the index of ' ' in fullname

#
if (space_pos == std::string::npos)
{
    throw std::runtime_error("Fullname with no space!");
}

To write good code we should now be a good programmer and check that the string we were given (fullname) actually contained a space. If it didn't then things get weird, is the fullname just the firstname, or maybe just the lastname? we don't know so we essentially give up

remote stratus
#

ah

bold pawn
#

So if we were given a fullname with no space the program will TERMINATE

remote stratus
#

I see

bold pawn
#
firstname = fullname.substr(0, space_pos);
surname = fullname.substr(space_pos + 1);

These lines are all about initializing our two data members. substr() (like find()) is a function you can call on any std::string. Its job is to create a new string from a 'sub-string' of fullname.

#

So the firstname is being set to the characters from index 0 (the start) all the way to just before the space_pos

#

the surname is being set from all the characters one after the position of the space

remote stratus
#

Aight I think I understand.

#

Ye I haven't gone that far into coding yet and haven't taught some of the things you said within my class yet. Still tryna get the hang of it

bold pawn
#

By the end of your class you should understand it

#

The main two things that are probably new for you to remember are constructors and exceptions

remote stratus
#

Ok

bold pawn
#

Google those words if you want to learn more. If they're teaching you c++ in class you'll learn these soon

remote stratus
#

ye for sure

#

I'm pretty sure most we covered so far is structures. And going over the broader things on how to do stuff in C++

#

We haven't gotten to much into the smaller details yet.

#

vectors, arrays, structures, strings and loops is what I been taught so far

#

But thanks for your help tho

#

👍

glacial sorrelBOT
#

Thank you and let us know if you have any more questions!

bold pawn
#

I'd recommend you use std::string as much as your teacher lets you. Don't use c-style strings like const char* my_string = "Hello!"; or char my_name[40];

remote stratus
#

kk

#

whats the issue with those kind of c-style strings?

#

Is it just inefficient in the sense of it being redundant code that could be simpler or what

bold pawn
remote stratus
#

I'll keep that in mind. At the same time not like I can really avoid using pointers when I'm being told to use them.