저는 C++에 익숙하지 않고 그것에 대한 첫 강의를 듣고 있습니다. 나는 "HelloHowAreYouToday"와 같은 사용자 입력을 받아들이는 프로그램을 만들 필요가있다. 그리고 각 대문자에서 그것을 소문자로 바꾸고이 "Hello 안녕하십니까?"처럼 보이는 C 문자열을 분리해야한다. 지금까지 제 코드가 있습니다.'+'와 함께 C++ C 문자열 분리
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
char sentence[100];
int size;
cout << "Enter a sentence with no spaces, make first letter in each word an upper case: ";
cin.getline(sentence, 100);
size = strlen(sentence);
char * manipSent = new char[size + 1];
for (int i = 1; i < size + 1; i++)
{
if (sentence[i] >= 'A' && sentence[i] <= 'Z')
{
manipSent[i] = ' ';
manipSent[i] = tolower(sentence[i]);
}
else;
manipSent[i] = tolower(sentence[i]);
}
manipSent[0] = sentence[0];
manipSent[size] = NULL;
cout << endl;
cout << "Original Sentence: " << sentence << endl;
cout << endl;
cout << "Altered Sentence: " << manipSent << endl;
delete[] manipSent;
return 0;
}
I가 단어를 분할되는 데 문제는, 아웃 풋은 "Hellohowareyoutoday"처럼 보이지만 나는 단어 사이에 공백을 추가 할 때 각 단어의 첫 글자가 삭제됩니다. 모든 입력을 부탁드립니다.
공간을 쓰고 난 후 'i'가 증가하지 않으므로 즉시 덮어 쓰게됩니다. 할당 할 때 공백을 고려하지 않으므로 버퍼가 너무 작습니다. 대신'std :: string'을 사용하십시오. –
1 단계 : char 버퍼 대신'std :: string'을 사용하십시오. 2) 공간을 삽입하십시오. 3 단계) ??? 4 단계) 이익. –