What if I don't know the length of the string to be entered in c/c++?

Asked 2 years ago, Updated 2 years ago, 69 views

When you say that an input is always entered as a string I don't know the length of the string to be entered I need to determine the size of the char array, what should I do? If the size is input first, it can be implemented as a dynamic variable, but if the size is not input, is it only using the stl?

I don't know what stl programming is, so if you use stl(?), I'd appreciate it if you could comment on it!

c c++ char

2022-09-22 08:18

1 Answers

In C/C++, there are several ways to represent a string.

In the case of A string ending in NULL, as you mentioned, you need to dynamically expand the space in which the string will be stored in dynamic memory.

On the other hand, std::string in the Standard Template Library (STL) has a convenient member function for managing strings and automatically expands memory when string length changes.

In addition, there are various classes for managing strings, such as CSstring and QString.

Among these, we will explain based on the NULL string ending in NULL and std::string standard for C++.

As you mentioned, this method allocates dynamic memory to store strings of unknown length. Of course, std::scanf saves the string before the space.

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int main() {
    char* input = NULL;
    {
        char value;
        int size = 0;
        int maxSize = 1;

        input = new char[maxSize + 1];
        while ((value = std::getchar()) != EOF) {
            if (std::isspace(value))
                break;
            if (size == maxSize) {
                char* temp = new char[2 * maxSize + 1];
                input[size] = '\0';
                std::strcpy(temp, input);
                delete[] input;
                input = temp;
                maxSize = 2 * maxSize;
            }
            input[size++] = value;
        }
        input[size] = '\0';
    }
    // ...
    delete[] input;
    return 0;
}

std::getchar() reads the entered string one by one from stdin. Verify that the characters read by std::isspace are blank and save to input. Because the length of the string to be entered is unknown in advance, if you do not have enough space for the string to be stored, such as char* temp = new char[2 * maxSize + 1];, you must be allocated a new size of memory.

The simplest way to read a string from C++.

#include <iostream>
#include <string>

int main() {
    std::string input; // string object
    Read the string from std::cin >> input; // stdin and save it to input
    // ...
    return 0;
}

Read the string from std::cin through >> operator and save it to input.

Even if you don't know the length of the string you entered, you can insert the string before the space. In practice, the tasks described above are done internally.

This method is similar to receiving a string using the dynamic memory described earlier. The difference is that you use std::string. push_back() and add one character to input. You don't have to worry about securing memory because std::string takes care of it.

#include <cctype>
#include <cstdio>
#include <string>

int main() {
    std::string input; // string object
    {
        char value;
        while ((value = std::getchar()) != EOF) {
            if (std::isspace(value))
                break;
            input.push_back(value); // Add characters stored in value at the end of input
        }
    }
    // ...
    return 0;
}

If you use C, you can use the same method as the first one. If you use C++, the second method is the most convenient.


2022-09-22 08:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.