可视易C++ = 数组长度和反向 WITHOUT <string>

visual Easy C++ = array length and reverse WITHOUT <string>

本文关键字:WITHOUT gt lt string C++ 数组 可视      更新时间:2023-10-16

我似乎无法得到我的数组的长度或向后打印!请帮忙?!
下面的函数void getstringlength和Print Backwards不起作用

#include <iostream>
void ReadString(char* c, int maxLength);
void GetStringLength(char* c, int* length);
void PrintString(char* const c);
void PrintStringBackwards(char* const c);
int main()
{
    const int SIZE = 50;
    char ca[SIZE];
    char* pc = ca;
    int fPrints = 0;
    int bPrints = 0;
    int lengthChecks = 0;
    char selection = 'z';
    while (selection != 'Q') {
        std::cout << "n[ 1] Test ReadStringn";
        std::cout << "[ 2] Test GetStringLengthn";
        std::cout << "[ 3] Test PrintStringn";
        std::cout << "[ 4] Test PrintStringBackwardsn";
        std::cout << "[Q] Quitn";
        std::cout << "Selection: ";
        std::cin >> selection;
        std::cin.ignore();
        std::cout << std::endl;
        switch (selection) {
        //Test ReadString
        case '1':
            ReadString(pc, SIZE);
            break;
        //Test GetStringLength
        case '2': {
            lengthChecks += 1;
            int length = 0;
            GetStringLength(pc, &length);
            std::cout << "Length[" << lengthChecks << "]=" << length << std::endl;
            break;
        }
        //Test PrintString
        case '3':
            fPrints += 1;
            std::cout << "Foward[" << fPrints << "]=";
            PrintString(pc);
            std::cout << std::endl;
            break;
        //[ 4] Test PrintStringBackwards
        case '4':
            bPrints += 1;
            std::cout << "Backwards[" << bPrints << "]=";
            PrintStringBackwards(pc);
            std::cout << std::endl;
            break;
        case 'Q':
            break;
        default:
            break;
        } //end switch
    } //end while
    std::cout << "Press ENTER";
    std::cin.get();
    return 0;
}
void ReadString(char* c, int maxLength)
{
    std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;
    std::cin.getline(c, maxLength, 'n');
}
//BELOW THIS DOESNT WORK EITHER///
//////////////////////////////////////////
void GetStringLength(char* c, int* length)
{
    for (int i = 0; i < *length; i++) {
        if (c[i] == '')
            *length = i - 1;
    }
}
void PrintString(char* const c)
{
    int counter = 0;
    for (int i = 0; i < 100; i++) {
        if (c[i] == '') {
            counter = i;
            break;
        } //end if
    } //end for
    for (int j = 0; j < counter; j++) {
        std::cout << c[j];
        if (j == counter)
            std::cout << '';
    } //end for
    std::cout << std::endl;
} //end void
void PrintStringBackwards(char* const c)
{
    //this is where I’m lost! I’ve tried 25 different ways and everything is error.
}

考虑for (int i = 0; i < *length; i++),其中i*length都是0,如您的情况…这个循环会执行吗?不…考虑一个循环,当str[x]''时,x设置为0return x;。例如,strlen中的循环就是这样做的。

至于向后打印,从最高索引(由strlen或函数一旦固定返回)开始递减,直到到达0,在这些偏移量处打印字符。

顺便说一下,strlen<cstring>中可用,而不仅仅是<string>…由于它是一个强制函数,它将始终是任何c++实现的一部分。你应该直接使用strlen

要获得C字符串(char*)的长度,您可以使用strlen(...)。要向后打印,请执行:

for(int i = strlen(str) - 1; i >= 0; --i)
    std::cout << str[i];