显示字符串的最后n个字符

Display last n characters of a string

本文关键字:字符 最后 字符串 显示      更新时间:2023-10-16

我需要关于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库中的函数。