读书人

初学者有关问题救c++字符串

发布时间: 2012-03-02 14:40:28 作者: rapoo

菜鸟问题,急救,c++字符串
读取一行文本,将所有4字单词替换成love,若4字单词开头字母是大写就替换成Love,然后输出

[解决办法]
#include "stdafx.h "
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

ifstream infile;
infile.open( "temp.txt ");


char * str1 = new char[200];
string str;
char * temp;
infile.getline(str1,200);


temp = strtok(str1, ";., ");
while(temp != NULL)
{
string tempstr(temp);
if(tempstr.length() == 4)
{
if(*temp> 'Z ')
{
cout < < "love " < < endl;
str =str + "love ";
}
else
{
cout < < "Love " < < endl;
str = str + "Love ";
}
}
else
{
cout < < temp < < " " < < endl;
str = str + temp + " ";
}
temp = strtok(NULL, ";., ");
}

cout < < str < < endl;
}

读书人网 >C++

热点推荐