可视易C++ = 数组长度和反向 WITHOUT <string>
visual Easy C++ = array length and reverse WITHOUT <string>
我似乎无法得到我的数组的长度或向后打印!请帮忙?!
下面的函数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
设置为0
和return 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];
相关文章:
- EASTL矢量<向量<int>>连续的
- dynamic_cast without RTTI
- Qt - QQuickWidget setSource without freezing UI
- "in-situ without memory allocation" 字符串的愚蠢实现意味着什么?
- C++线程"Call to non-static member function without an object argument"
- "How to make a recursive call for palindrom numbers without reverse function in c++?"
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 如何使用 std::transform with container without push_back.
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- Poco::Net::HTTPClientSession throw exception without unique_
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- C++线程未加入"Termination called without active exception"