urallsad
urallsad

Reputation: 115

Copy single characters in C-string without pointers

Here is the assignment:

Create a Cstring variable that contains a name, age, and title. Each field is separated by a space. For example, the string might contain “Bob 45 Programmer” or any other name/age/title in the same format. Write a program using only functions from cstring (not the class string ) that can extract the name, age, and title into separate variables.

I extracted the name from the string, but I'm having trouble with getting any character after that. I can't use pointers because we haven't learned that yet so no strtok. I just need a direction to go because I am sure there is a function to make this easier. Thank you.

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char y[] = "taylor 32 dentist";
    char name[25];
    char age[4];
    char title[40];
    int i = 0;
    while (y[i] != ' ')
    {
        while (y[i] != '\0')
        {
            if (y[i] == ' ' || y[i + 1] == '\0')
            {
                break;
            }
            name[i] = y[i];
            i++;
        }
    }
    cout << "Name: " << name << endl
         << "Age: " << age << endl
         << "Title: " << title << endl;
    return 0;
}

Solved:

#include <iostream>
#include <cstring>

using namespace std;

void separate_Variables(char y[], char name[], char age[], char title[]);

void output(char name[], char age[], char title[]);

int main()
{
    char y[] = "taylor 32 dentist";
    char name[25];
    char age[4];
    char title[40];
    separate_Variables(y, name, age, title);
    output(name, age, title);
    return 0;
}

void separate_Variables(char y[], char name[], char age[], char title[])
{
    int i = 0;
    int j = 0;
    while (y[i] != '\0' && y[i] != ' ') {
        name[j++] = y[i++];
    }
    name[j] = '\0';
    j = 0;
    i++;
    while (y[i] != '\0' && y[i] != ' ') {
        age[j++] = y[i++];
    }
    age[j] = '\0';
    j = 0;
    i++;
    while (y[i] != '\0' && y[i] != ' ') {
        title[j++] = y[i++];
    }
    title[j] = '\0';
    j = 0;
}

void output(char name[], char age[], char title[])
{
    cout << "Name: " << name << endl
         << "Age: " << age << endl
         << "Title: " << title << endl;
}

Upvotes: 0

Views: 1061

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

You do not need nested loops - all you need is three loops one after the other.

The loops would look the same: take i-th character, compare it to space, and store in one of three destinations. When you see space, replace it with '\0', and move on to the next destination:

int j = 0;
while (y[i] != '\0' && y[i] != ' ') {
    name[j++] = y[i++];
}
name[j] = '\0'; // Add null terminator
j = 0;          // Reset j for the next destination
i++;            // Move to the next character in y[]
... // Do the same thing for age and title

Upvotes: 3

mksteve
mksteve

Reputation: 13073

The outer loop needs to be testing for '\0'

The inner loop (now != ' ') can be duplicated for each field. You need a separate index variable to separate i for y and where to put it for field (fi?)

Upvotes: 0

Related Questions