2016-09-26 52 views
0

我需要有關C字符串和函數的分配方面的幫助。顯示字符串的最後n個字符

#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設置,用戶輸入數字前的每個字符都爲0,但所做的全部字符串都是空的。

難道有人請向我解釋一下,爲了創建一個能顯示字符串最後n個字符的函數,我需要採取的步驟。我試過在網上和我的書上看,但它並沒有真正幫助我。

+0

你想只顯示或顯示和修改原始? – Zeokav

+0

我只想顯示用戶輸入的號碼後的字符。所以如果字符串是「Hello there」,並且用戶想要顯示字符串的最後3個字符,那麼只有「ere」會顯示在main中。 –

回答

1

指向char的指針通常被認爲是C字符串。例如,格式化輸出的運算符(operator<<()這樣做)。 C字符串遵循特定的約定:它們被遇到的第一個空字符終止。當一個數組被用在一個指向一個對象的指針的上下文中時,它們會「腐爛」成一個對應的對象。

要打印char數組的第一個字符nsentence只需要設置sentence[n]用它與輸出操作之前爲空。可以使用std::ostream的未格式化的輸出功能write(),相反,以避免修改字符串(例如,字符串不能被修改):

std::cout.write(sentence, n); 

打印的對象通常被認爲是一種非修改操作,即,I 「D使用未格式化的輸出打印第一n字符或一些東西,寫個性就像使用

std::copy(sentence, sentence + n, std::ostreambuf_iterator<char>(std::cout)); 

要打印的最後一個字符n你需要查找的字符串s的長度,確定是否s大於n,如果是這樣,只需開始打印sentence + (n - s)。您可以使用格式化或未格式化的方法來編寫最後的n字符。爲了對稱,我會用同樣的方法寫出第一個字符,但是從別處開始。

BTW,你應該始終確定如果你的輸入是讀取成功後,例如:

if (std::cin >> n) { 
    // use n 
} 
else { 
    // report a read failure and potentially try to recover 
} 
0

這是一個非常簡單的問題。你有一個起點,你需要顯示它直到字符串的末尾。

您可以遍歷從位置n開始的字符串的字符,直到您讀取一個空值(用於指示字符串的終止)。

char *display_last_nchar(char sent[], int n) 
{ 
    char buff[100]; 
    int i = strlen(sent) - n; char ch; 
    while((ch = sent[i])!='\0') 
    { 
    strcat(buff, ch); 
    } 
    strcat(buff, '\0'); 
    return buff; 
} 

字符數組(或任何類型的數組)被傳來傳爲指針,所以你應該使用返回類型爲一個字符指針,而不僅僅是字符。

該函數將返回指向存儲最後n個字符的緩衝區的指針。請記住,strlen和strcat是string.h庫中的函數。

0

使用C++字符串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 characters\n"; 
    return 0; 
} 

輸入

My hamster has a new toy 
5 

輸出

w toy 
相關問題