Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:25:31+00:00 2026-05-25T23:25:31+00:00

This code should ask the user for a their name and then split it

  • 0

This code should ask the user for a their name and then split it at the space.

It should put the firstname in the variable first, and the last name in de variable lastname

#include <iostream>

using namespace std;

int main()
{
char string[80];
char first[20];
char lastname[20];
bool f = true;
int c = 0;
cout << "Whats your Name? \n";
gets(string);

for(int i =0; i < strlen(string); i++){
    if(string[i] == ' ') {
        f = false;
        c = 0;
    }

    if(f) {
        first[c] = string[i];
    } else if(!f) {
        lastname[c] = string[i];
    }


    c++;
}

for(int i = 0; i < strlen(first); i++) {
    cout << first[i] << "\n";
}
for(int i = 0; i < strlen(lastname); i++) {
    cout << lastname[i]<< "\n";
}

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:25:32+00:00Added an answer on May 25, 2026 at 11:25 pm

    Unless you really need to write this using only C functions, it would be much easier to use C++ strings.

    Something like (this is untested):

    std::string input;
    std::string first;
    std::string lastname;
    
    // prompt the user
    std::cout << "What's your name? ";
    // get a line of input
    std::getline(std::cin, input);
    
    // find a space in the string
    size_t space = input.find_first_of(" ");
    // was the space found?
    if (space != std::string::npos)
    {
        // copy out the first and last names
        first = input.substr(0, space);
        lastname = input.substr(space + 1);
    
        // output them to stdout
        std::cout << first << std::endl << lastname << std::endl;
    }
    

    This means you don’t have to worry about null-terminating strings or string lengths or anything like that. As flolo said, your code doesn’t do that and thus will definitely run into problems. The memory layout of a C string is an array of characters with a null byte on the end, which is how things like strlen() know where the end of the string is. Also, your code’s going to have a horrible time the moment somebody enters a name with more than 20 characters in it, which isn’t particularly implausible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First check out this code. I seems like it should work for me, but
This code should i place this code at bottom of body it's in conditional
In theory, this code should set the new lock status to the users and
I have this code which should create a splash image with either no animation
The Google Analytics setup instructions state: This tracking code snippet should be included in
In this thread some one commented that the following code should only be used
I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
As far as I can tell, this code is fine, and should display some
Do you consider this a code smell? foreach((array)$foo as $bar) { $bar->doStuff(); } Should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.