1141:删除单词后缀--substr()

【题目描述】

给定一个单词,如果该单词以er、ly或者ing后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为0), 否则不进行任何操作。

【输入】

输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为32)。

【输出】

输出按照题目要求处理后的单词。

【输入样例】

referer

【输出样例】

refer

代码:


#include<iostream>
#include<cmath> 
#include<cstring>
#include<string>
#include<string.h>

using namespace std;
int main()
{
  
  
  string S;
  cin>>S;
  
  int len=S.length();
  

  
  if(S[len-2]=='e'&&S[len-1]=='r')
  {
  	S=S.substr(0,len-2);

   } 
   else{
    if(S[len-2]=='l'&&S[len-1]=='y')
      {
  	S=S.substr(0,len-2);
     }
     
     else{
     	 if(S[len-3]=='i'&&S[len-2]=='n'&&S[len-1]=='g')
  {
  	S=S.substr(0,len-3);
  	
   }
	 } 
   }
  cout<<S;
   
    return 0; 
}

S=S.substr(a,b);

a表示从S字符串的哪一个位置开始截取;b表示要截取的长度,当b为负数时就往前截取

返回一个字符串,不能直接S.substr(a,b);这样对S没有影响。

或者判断之后直接for循环(int i=0;i<len-2;i++)输出S[i]
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将据文件中的据制成折线图,需要先将据文件中的每一行按照空格分割成多个字符串,然后提取第二个字符串之后的据并存储到一个组中,最后使用gnuplot程序进行绘图。以下是使用C++和system调用gnuplot程序绘制折线图的示例代码: ```cpp #include <iostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; int main() { // 读取据文件 ifstream dataFile("data.txt"); string line; string data; while (getline(dataFile, line)) { size_t pos = line.find_first_of(" "); pos = line.find_first_not_of(" ", pos); data += line.substr(pos) + " "; } dataFile.close(); // 绘制折线图 string command = "gnuplot -e \"set term png; set output 'output.png'; plot '-' with lines;\""; FILE* gnuplotPipe = popen(command.c_str(), "w"); if (gnuplotPipe) { // 发送据 fputs(data.c_str(), gnuplotPipe); fputs("e\n", gnuplotPipe); fflush(gnuplotPipe); // 关闭管道 pclose(gnuplotPipe); } return 0; } ``` 在代码中,首先使用`ifstream`类读取据文件,并使用`getline()`函逐行读取据。然后,使用`find_first_of()`和`find_first_not_of()`函提取每行据中的第二个字符串之后的据,并将提取出的据拼接成一个字符串。接着,使用`system()`函调用gnuplot程序并设置绘图输出为PNG格式的文件,并使用管道向gnuplot程序发送绘图指令和据。最后,关闭管道。 需要注意的是,以上代码中使用了`popoen()`函和管道向gnuplot程序发送绘图指令和据,需要确保系统中已经安装了gnuplot程序。此外,具体实现可能需要根据据文件的格式和内容进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值