2017-01-31 7 views
0

ユーザ入力を他のユーザ入力に置き換える際に問題が発生しています。C文字列を扱うときに、最初の入力を現在の入力に置き換えるにはどうすればいいですか?

たとえば、ユーザーが「私は犬を愛している」と入力し、他の文字列を入力したいと尋ねて、「私はたくさんのアイスクリームを食べます」と入力します。

ユーザーの最初の入力を現在の入力に置き換えるにはどうすればよいですか?

// Function Prototype 
int countVowels(char * str); 
int countCons(char * str); 

int main() 
{ 

const int SIZE = 81;   // Max size of the array 
           //const int V_SIZE = 6;   // Size of the vowel array 
char newSentence[SIZE]; 
char userString[SIZE]; 
//char vowels[V_SIZE] = {'a', 'e', 'i', 'o', 'u'}; 
char choice;     // To hold the menu choice 
char *strPtr = userString; // Declare and initialize the pointer 
char *sentPtr = newSentence; 
           // Get the string from the user 

cout << "Please enter a string. (Maximum of 80 characters) :\n\n"; 
cin.getline(userString, SIZE); 
do{ 
    // Display the menu and get a choice 
    cout << "\n\nA. Count the number of vowels in the string \n"; 
    cout << "B. Count the number of consonants in the string \n"; 
    cout << "C. Enter another string \n"; 
    cout << "D. Exit the program \n\n"; 
    cout << "Please make your selection: "; 
    cin >> choice; 

    // Menu selections 
    if (tolower(choice) == 'a') 
    { 
     countVowels(userString); 
     cout << "This string contains " << countVowels(userString); 
     cout << " vowels. \n"; 
    } 
    else if (tolower(choice) == 'b') 
    { 
     countCons(userString); 
     cout << "This string contains " << countCons(userString); 
     cout << " consonants. \n"; 
    } 

    else if (tolower(choice) == 'c') 
    { 
     cout << "Please enter other string: "; 
     cin.getline(newSentence, SIZE); 
     userString.replace(); 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

    } 
    else 
    { 
     system("PAUSE"); 
     return 0; 
    } 

} while (choice != 'D'); 
} 

メニュー選択に問題があります。C:userStringをnewSentenceに置き換えるにはどうすればよいですか?

+2

コードがC++で記述されている場合は、cタグが不要です。 –

+0

どういう意味ですか? –

+0

あなたの質問から 'c''タグを' C++ '' c''文字列 'タグリストから削除するだけです。 –

答えて

1

あなたの目的に合わせて2つの配列宣言をする必要はありません。 cin.getline(newSentence、SIZE)の代わりに。 cin.getline(userString、SIZE);を使用します。新しい文字列を上書き保存します。

else if (tolower(choice) == 'c') 
{ 
    cout << "Please enter other string: "; 
    cin.getline(userString, SIZE); 
    cin.clear(); 
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

} 

他のものを探している場合は、それを定義します。

1

strcpyという名前のC言語には、ある文字列を別の文字列に上書きする関数が組み込まれています。

左の文字列が新しい文字列になります。

関連する問題