看不见的字符串出现在新的字符串中

时间:2016-04-14 05:02:47

标签: c++

我的代码无法将第一个字符串后的任何字符串的第一个字母大写,而不显示字符串的最后一个字。代码使用linux重定向从输入文件中读取一系列标题。每个单词的第一个字母需要大写,其余字必须小写,每个单词需要用一个黑色空格分隔。这是测试数据的输出:

The  5th  
the  Cat In The 
one Fish Two Fish  Red Fish Blue

还要注意第5和第5和鱼和红之间有两个空格

#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;

void reform (string& imup);
int main ()
{
     string word;
     string temp = " ";
     string letters;
     getline(cin,letters);
     int length;
     length = 0; 
     word = "";
     while (cin)
     {  
        length = letters.size();
        for (int i =0; i < length; i++)
        {

            if (letters[i] != ' ' && letters[i] != '\n')
            {
                word = word + letters[i];
            }
            else if ((letters[i] == ' ' && isalpha(letters[i-1])) ||
                     (letters[i] == ' ' && isalpha(letters[i+1]))) 
            {
                reform(word); 
                cout<< word <<" " ;
                word = "";
            }
            else if ((letters[i] == ' ' && isdigit(letters[i-1])) ||
                     (letters[i] == ' ' && isdigit(letters[i+1]))) 
            {
                reform(word); 
                cout<< word <<" " ;
                word = "";
             }

         }
         cout<< endl;
         getline(cin,letters);
     }

     return 0;
}


void reform (string& imup)
{
    int size;
    // cout <<"hi "; 
    size = imup.length();
    imup[0] = toupper(imup[0]);
    for (int i=1; i < size; i++)
         imup[i] = tolower(imup[i]);
}

2 个答案:

答案 0 :(得分:0)

给定a__b作为输入(下划线表示空白),这将在a后面的空白处识别单词end,打印a_。然后它还会在b之前的空白处检测到单词结束,打印_,因为单词是空的。

我不确定为什么你在空白后看字符。在检查前一个字符之前,您还应该确保我不是0。

为什么不在输入空白或输入结束时输出单词是否非空?

类似的东西:

while (getline(cin, letters)) {
  bool first = true;
  string word;
  for (string::const_iterator s = letters.begin(); s != letters.end(); ++s) {
    if (*s == ' ' || *s == '\r' || *s == '\n') {
      if (!word.empty()) {
        if (!first) cout << ' ';
        reform(word);
        cout << word;
        word = "";
        first = false;
      }
    }
    else
      word += *s;
  }
  if (!word.empty()) {
    if (!first) cout << ' ';
    reform(word);
    cout << word;
  }
  cout << endl;
}

第一个标志然后避免在一行上打印尾随空白。

答案 1 :(得分:0)

这是我的代码版本(您可以使用迭代器替换 for 循环):

My testng.xml file is:- 

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" thread-count="4" parallel="tests">

    <test name="PVR Test">
    <parameter name="remoteurl" value="http://localhost:5555/wd/hub" />
        <classes>
            <class name="dd_testcases.login">
                <methods>
                    <include name="banner_check" />
                </methods>
            </class>
        </classes>
    </test> <!-- Test -->

    <test name="footersection">
    <parameter name="remoteurl" value="http://localhost:5556/wd/hub" />

        <classes>
            <class name="dd_testcases.News_General_Footer">
                <methods>
                    <include name="News_General_Footer" />
                </methods>
            </class>
        </classes>
    </test>
    Test
</suite> <!-- Suite -->

and my code is :-

    @BeforeSuite

    @Parameters("remoteurl")

    public void init(String remoteurl) throws IOException, InterruptedException{
        //BasicConfigurator.configure();
        dbcon=new sqldbconfig();
        logs=Logger.getLogger("PVR");
        config=new Properties();
        OR=new Properties();

        if (driver==null){

            InputStream is = getClass().getResourceAsStream("/config.properties");
            config.load(is);
            //fis=new FileInputStream(config.getProperty("confpath"));

            fis=new FileInputStream(System.getProperty("user.dir")+config.getProperty("ORpath"));
            //fis=new FileInputStream(System.getProperty("user.dir")+"\\src\\dd_properties\\OR.properties");
            OR.load(fis);

            //fis=new FileInputStream(config.getProperty("xlspath"));   
            excel=new Xls_Reader(System.getProperty("user.dir")+config.getProperty("xlspath"));
            System.out.println("Browser:: "+config.getProperty("Browser"));
            if (config.getProperty("Browser").equalsIgnoreCase("Mozilla")){
                cap=DesiredCapabilities.firefox();
                cap.setBrowserName("firefox");
                cap.setPlatform(Platform.ANY);
            }
            else if(config.getProperty("Browser").equalsIgnoreCase("chrome")){
                cap=DesiredCapabilities.chrome();
                cap.setBrowserName("chrome");
                cap.setPlatform(Platform.ANY);  
            }
        driver=new RemoteWebDriver(new URL(remoteurl),cap);
            driver.get(config.getProperty("testurl"));

and when i run via testng.xml file code throws error:-

org.testng.TestNGException: 
Parameter 'remoteurl' is required by @Configuration on method init but has not been marked @Optional or defined
in C:\Users\HT1\workspace\PVRGrid-A\testng.xml
    at org.testng.internal.Parameters.createParameters(Parameters.java:148)
    at org.testng.internal.Parameters.createParameters(Parameters.java:361)
    at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:84)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:197)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:296)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
    at org.testng.TestNG.run(TestNG.java:1018)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Please help me.....what i am doing wrong....