C++ 如何处理c字符串指针
我目前正忙于第一部分C++ 如何处理c字符串指针,c++,loops,pointers,c-strings,C++,Loops,Pointers,C Strings,我目前正忙于第一部分 int main () { int const SIZE = 500; char sentences[SIZE]; char* pointers[SIZE]; do { cout<<"Please enter small sentences, hit enter to continue or 0 to stop: "<<endl; cin.getline(sentences, 30); *pointers = se
int main () {
int const SIZE = 500;
char sentences[SIZE];
char* pointers[SIZE];
do {
cout<<"Please enter small sentences, hit enter to continue or 0 to stop: "<<endl;
cin.getline(sentences, 30);
*pointers = sentences;
cin.ignore();
} while (!(cin.getline>>0));
system ("PAUSE");
return 0;
int main(){
int const SIZE=500;
字符句子[大小];
字符*指针[大小];
做{
cout您没有向数组追加字符,也没有向指针数组追加指针
以下是您需要执行的一些步骤:
1.在“句子”数组中维护指向下一个可用位置的索引或指针。
2.从文件中读取字符串后,将其内容复制到“句子”数组中的下一个可用位置。请参阅std::string::c_str()或std::string::data()将字符串转换为“char*”
3.维护指向指针数组中下一个可用位置的指针或索引。
4.将句子指针复制到指针数组中的下一个可用位置。
5.将句子指针前移字符串长度。
6.将指针向前移动一个指针
未提供代码,因为这将破坏分配的目的。扰流板警报
#include <cstring>
#include <iostream>
int main() {
const int SIZE = 500;
char sentenceBuffer[SIZE] = {0};
char* sentences[SIZE / 2] = {0}; // shortest string is a char + '\0'
int sentenceCount = 0;
char* nextSentence = &sentenceBuffer[0];
std::cout << "Enter sentences. Enter 0 to stop.\n";
while (std::cin.getline(nextSentence, SIZE - (nextSentence - &sentenceBuffer[0]))) {
if (0 == std::strcmp(nextSentence, "0")) {
break;
}
sentences[sentenceCount] = nextSentence;
++sentenceCount;
nextSentence += std::cin.gcount(); // '\n' stands in for '\0'
}
for (int i = sentenceCount - 1; i != -1; --i) {
std::cout << sentences[i] << "\n";
}
return 0;
}
#包括
#包括
int main(){
常数int SIZE=500;
char sentenceBuffer[SIZE]={0};
char*句[SIZE/2]={0};//最短的字符串是char+'\0'
int-sentenceCount=0;
char*nextcontence=&sentenceBuffer[0];
std::cout这里有一个提示-声明基本数据,您需要对这些数据进行操作的方法:
class Buffer //buffer data combined with methods
{
private:
char* buffdata; //buffer to hold raw strings
int buffsize; //how big is buffdata
int bufflast; //point to null terminator for last saved string
char** ray; //declare ray[raysize] to hold saved strings
int raysize; //how big is ray[]
int raycount; //count how many strings saved
//methods here
public:
Buffer() //constructor
{ // you figure out what goes here...
}
~Buffer() //destructor
{ // release anything stored in buffers
}
add(char *str); //define method to add a string to your data above
get(unsigned int index); //define method to extract numbered string from stored data
};
你尝试过什么吗?你有什么错误吗?你不理解的第一个细节是什么?我不能做DoT while循环来开始工作,这是我尝试的代码,但没有运气,它是在C++中。谢谢int main(){int const大小=500;字符句子[大小]。;字符*指针[大小];do{coutalso,我是新来的,所以我不熟悉评论的格式,希望我发布的最后一条评论是可读的。@GamalielTellezOrtiz,最好是将源代码放入问题中。在您的情况下,您将源代码的副本放入评论中,而不是编辑您的问题以将其添加到问题中。将来,编辑您的问题,以提供有关您的问题的其他信息,而不是添加评论。这样,所有相关信息都会集中在一个位置。非常感谢,我将通篇查看以获得更好的效果understand@GamalielTellezOrtiz,我希望您自己尝试解决它,如果遇到问题,请回到我的解决方案。const char*const endOfBuffer=(&sentenceBuffer)[1];
,那么剩余的字符数就是endOfBuffer-nextcontence
您也可以使用cin.gcount()
而不是strlen(nextcontence)
@AdamBurry,绝对是因为我绝对想成为一名优秀的程序员。我正在尝试自己做这件事,并请您给予指导。再次感谢