显示字符串的最后n个字符
Display last n characters of a string
我需要关于c-string和函数的赋值的帮助。
#include <iostream>
using namespace std;
//other functions
char display_last_nchar(char sent[], int n); // function i'm having trouble with
void main()
{
char sentence[31];
int selection, n;
do {
cout << "Please enter a string: " << endl;
cin.getline(sentence, 31, 'n'); //also, for me here i have to hit enter twice. How can I fix that?
cin.ignore();
cout << "Please make a selection: " << endl;
//other options
cout << "4. Display the last n character of the string " << endl;
cout << "7. Exit" << endl;
cin >> selection;
cin.ignore();
switch (selection)
{
case 4:
cout << "How many characters from the end of the string "
<< "do you want to display? : " << endl;
cin >> n;
cin.ignore();
if (n >= 30)
{
cout << "Error: too many characters" << endl;
break;
}
else
display_last(sentence, n);
cout << sentence << endl;
break;
case 7:
break;
}
}
while (choice != 7);
//other functions
char display_last_nchar(char sent[], int n)
{
for (int i = n; n > 30; i++)
{
sent[i]; //I know this is wrong but this is a guess that i took
}
return sent[n];
}
我知道如何显示字符串的前n个字符。对于这个函数,如果用户输入一个名为"我的仓鼠有一个新玩具"的字符串,并且他们想要显示前8个字符,那么它会将第8个字符之后的所有字符设置为空,因此只显示"我的火腿"。
我想做的是,对于display_last_nchar设置,用户输入number之前的每个字符都为0,但所做的只是将整个字符串空。
有人可以向我解释一下,我需要采取的步骤,以创建一个函数,将显示字符串的最后n个字符。我试着在网上和我的书里找过,但都没有真正帮助我。
使用c++ string STL。substr
方法在这里很有用。例如,要输出字符串
n
字符#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
getline(cin, s);
cin >> n;
if (n < s.length())
cout << s.substr(s.length() - n) << endl;
else
cout << "Not enough charactersn";
return 0;
}
输入My hamster has a new toy
5
w toy
指向char
的指针通常被认为是c字串。例如,用于格式化输出的操作符(operator<<()
这样做)。C字符串遵循一个特定的约定:它们以遇到的第一个空字符结束。当数组在需要对象指针的上下文中使用时,它们会"衰减"为相应的对象。
要打印char
数组sentence
的第一个n
字符,只需要在使用输出操作符之前将sentence[n]
设置为null。您可以使用std::ostream
的未格式化输出函数write()
,以避免修改字符串(例如,字符串字面值不能修改):
std::cout.write(sentence, n);
打印一个对象通常被认为是一个非修改操作,也就是说,我将使用未格式化的输出来打印第一个n
字符,或者像使用
std::copy(sentence, sentence + n, std::ostreambuf_iterator<char>(std::cout));
要打印最后一个n
字符,您需要找到字符串s
的长度,确定s
是否大于n
,如果是,则从sentence + (n - s)
开始打印。您可以使用格式化或未格式化的方法来编写最后一个n
字符。为了对称,我将使用相同的方法来编写第一个n
字符,但从其他地方开始。
顺便说一句,你应该总是在读取后确定你的输入是否成功,例如:
if (std::cin >> n) {
// use n
}
else {
// report a read failure and potentially try to recover
}
这是一个相当简单的问题。你有一个起始点,你需要显示它直到字符串结束。
您可以从位置n开始遍历字符串的字符,直到读取null(用于表示字符串的终止)。
char *display_last_nchar(char sent[], int n)
{
char buff[100];
int i = strlen(sent) - n; char ch;
while((ch = sent[i])!=' ')
{
strcat(buff, ch);
}
strcat(buff, ' ');
return buff;
}
字符数组(或任何类型的数组)作为指针传递,因此您使用的返回类型是char指针,而不仅仅是char。
这个函数将返回指向存储最后n个字符的缓冲区的指针。记住,strlen和strcat是string.h库中的函数。
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 使用连续分隔符和空最后一个字符进行拆分
- CPP:如何获取字符的最后一个索引
- 如何检查字符串中的最后一个字符是否是某个字符并将其从字符串中删除?(C++)
- 比较最后一个字符,不区分大小写(带谓词?
- 在弦乐器中删除最后一个字符
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- 在C 字符串中,为什么在最后一个字符之后,通过索引和()访问索引时行为是不同的
- 如何查看复杂度为 O(1) 的字符串流对象中的最后一个字符
- 最后一个字符行编辑
- C 字符串副本()在最后给我额外的字符,为什么是这样
- 仅当捕获组最后一次出现时,才使字符成为可选字符
- 如何在C 中获得字符串的最后一个字符
- 从字符串中删除字符的第一个和最后一个实例
- 删除文件 C++ 中字符串中最后一个出现的字符
- 如何在C++中删除 UTF-8 字符串的最后一个字符
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词
- 如何获取输入文件的行的第二个和最后一个字符,并在C++中组成数组
- 删除字符C++最后一次出现时字符数组的开头
- 返回常量字符数组中字符最后出现的索引