C++大学教程练习(3)字符串处理(重要的)

反序打印字符串
(并且将大写字母转化为小写字母,并且将小写字母转换为大写字母)

#include<iostream>
#include<cstdio>
using namespace std;
void reverse(char *str)
{
	char *temp=new char[strlen(str)+1];
	for(int i=strlen(str)-1,j=0;i>=0;i--,++j)
	{
		temp[j]=str[i];
	}
	strcpy(str,temp);
}
void transform(char *str)
{
	for(int i=0;i<strlen(str);i++)
	{
		if(str[i]>='A'&&str[i]<='Z')
			str[i]=str[i]+32;
		else if (str[i]>='a'&&str[i]<='z')
			str[i]=str[i]-32;
		else
			continue;
	}
}
int main()
{
	char str[100];
	while(gets(str))
	{
		cout<<str<<endl;
		reverse(str);
		cout<<str<<endl;
		transform(str);
		cout<<str<<endl;
	}
	return 0;
}

在这里插入图片描述
密码问题(就是用上面那个方法),再加一个 x代表位移的步数
s[i]=(s[i]-‘a’+x)%26+‘a’
数回文词

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
bool huiwen (string word)
{
	int left=0;
	int right=word.length()-1;
	while(left<right)
	{
		if(word[left++]!=word[right--])
		{
			return false;
		}
	}
	return true;
}
int main()
{
	string str;
	int count=0;
	getline(cin,str);
	int pos=0;
	while(pos!=string::npos)
	{
		pos=str.find_first_of(" ",0);
		string temp=str.substr(0,pos);
		if(huiwen(temp))
			count++;
		str.erase(0,pos+1);
	}
	cout<<"一共有"<<count<<"个回文单词"<<endl; 
	return 0;
}

在这里插入图片描述
数元音

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
	string str;
	getline(cin,str);
	int count=0;
	int pos=str.find_first_of("aoiue",0);
	while(pos!=string::npos)
	{
		count++;
		pos=str.find_first_of("aoiue",pos+1);
	}
	cout<<count<<endl;
	return 0;
}

字符串插入在字符串中间插入

int main()
{
	string str;
	getline(cin,str);
	int pos=str.length()/2;
	str.insert(pos,"******");
	cout<<str<<endl; 
	return 0;
} 

反序输出(begin,end)
字母金字塔
这个输出为对齐边的

#include<iostream>
#include<string>
using namespace std;
string reverse(string str)
{
	string temp;
	for(int i=str.length()-1;i>=0;i--)
	{
		temp+=str[i];
	}
	return temp;
}
int main()
{
	string str="abcdefghijklmnopqrstuvwxyz{";
	int k=str.length()/2;
	int time=0; // 中心为为2k
	while(time<=k)
	{
		string temp="";
		string t1=str.substr(time,time);
		
		temp+=t1;
		temp+=str.substr(2*time,1);
		temp+=reverse(t1);
		cout<<temp<<endl;
		
		time++;
		
	}
	cout<<endl;
	return 0; 
}

这个是和题上一致的

#include<iostream>
#include<string>
using namespace std;
string reverse(string str)
{
	string temp;
	for(int i=str.length()-1;i>=0;i--)
	{
		temp+=str[i];
	}
	return temp;
}
int main()
{
	string str="abcdefghijklmnopqrstuvwxyz{";
	int k=str.length()/2;
	int time=0; // 中心为为2k
	while(time<=k)
	{
		string temp="";
		string t1;
		for(int i=0;i<13-time;i++)
		{
			t1+=" ";
		}
		t1+=str.substr(time,time);
		temp+=t1;
		temp+=str.substr(2*time,1);
		temp+=reverse(t1);
		
		cout<<temp<<endl;
		time++;
	}
	cout<<endl;
	return 0; 
}

在这里插入图片描述
将字符串转化为浮点数
(C++大学教程的答案)

#include <iostream> 
#include <iomanip> 
#include <cstdlib>
using namespace std;

const int SIZE = 15;

int main()
{
   char stringValue[ SIZE ];
   double sum = 0.0;
   
   for ( int i = 1; i <= 4; i++ )
   {
      cout << "Enter a floating-point value: ";
      cin >> stringValue;
      sum += atof( stringValue ); // convert to double
   } // end for

   cout << fixed << "\nThe total of the values is " << setprecision( 3 ) 
      << sum << endl;
} // end main

搜索子串
strstr(str,substr)
若substr是str的子串,返回首次出现的地址;不是str的子串,返回false.(首地址包含后面的字符串哦)

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char text[20];
	char stext[10];
	cin.getline(text,20);
	cin.getline(stext,10);
	char *sptr;
	sptr=strstr(text,stext);
	if(sptr)
	{
		cout<<sptr<<endl;
		if(sptr=strstr(sptr+1,stext))
		{
			cout<<sptr<<endl;
		}
	}
	else
	{
		cout<<"cant find substring"<<endl;
	}
	return 0;
}

在这里插入图片描述
搜索子串2,在几行文字中查找一个字符串,并确定该字符串在这些文字中的总次数,显示结果。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char line[3][80];
	cout<<"your line: "<<endl;
	for(int i=0;i<3;i++)
	{
		cin.getline(&line[i][0],80);
	}
	cout<<"your search substring: "<<endl;
	char substring[40];
	cin.getline(substring,40);
	int count=0; //记录字符串出现的次数 
	for(int i=0;i<3;i++)
	{
		char *sptr;
		sptr=strstr(&line[i][0],substring);
		while(sptr)
		{
			count++;
			sptr=strstr(sptr+1,substring);
		}
	}
	cout<<substring<<"出现了"<<count<<"次"<<endl;
	return 0;
}

在这里插入图片描述
记电话号码
(用strtok来分割字符串)

#include <iostream> 
#include <cstring> 
using namespace std;

int main()
{   
   char p[ 20 ];
   char phoneNumber[ 10 ];
   
   char *tokenPtr; 
   char *areaCode; 
   char *phone; 
   
   cout << "Enter a phone number in the form (555) 555-5555:\n";
   cin.getline( p, 20 );
   areaCode = strtok( p, "()" );
   tokenPtr = strtok( 0, "-" ); // 前三位 
   strcpy( phoneNumber, tokenPtr );
   tokenPtr = strtok( 0, "" ); //后四位 
   strcat( phoneNumber, tokenPtr );
   phone = phoneNumber;
   cout << "\nThe area code is " << areaCode 
      << "\nThe phone number is " << phone << endl;
}

翻转一行英文句子
(用strtok来分割,用char *mark[]来存储每个单词)

#include<iostream>
#include<cstring>
using namespace std;
void reverse(char*);
int main()
{
	char str[80];
	cout<<"Enter a line of sentence"<<endl; 
	cin.getline(str,70);
	reverse(str);
	cout<<endl;
	return 0;
}
void reverse(char *str)
{
	char *mark[50]; // 放每个单词
	char *temp;
	int count=0;
	temp=strtok(str," "); // 每个句子以空格为分割
	while(temp!=NULL)
	{
		mark[count++]=temp;
		temp=strtok(NULL," ");
	}
	for(int i=count-1;i>=0;i--)
	{
		cout<<mark[i]<<" ";
	}
	cout<<endl;
}

 

文本处理
partA:
从键盘中读入几行文本,并打印一个表格,表格显示字母表中每个字母在文本中出现的次数。

#include<iostream>
#include<cstdio>
#include<iomanip> 
using namespace std;

int main()
{
	char letters[26]={};
	char text[3][80];
	char i;
	cout<<"Enter three sentence"<<endl;
	for(int i=0;i<=2;i++)
	{
		cin.getline(&text[i][0],80);// 读入一行 
	} 
	for(int i=0;i<=2;i++)
	{
		for(int j=0;text[i][j];j++)
		{
			if(isalpha(text[i][j]))
				letters[tolower(text[i][j])-'a']++;
		}
	} 
	cout << "\nTotal letter counts:\n";
	for(int i=0;i<26;i++)
	{
		cout<<setw(3)<<static_cast<char>('a'+i)<<" : ";
		cout<<setw(3)<<static_cast<int>(letters[i])<<endl;
	}
} 

在这里插入图片描述
PartB
(输入的句子的打单词长度表格)

#include<iostream>
#include<cstdio> 
#include<iomanip>
using namespace std;
int main()
{
	char line[3][80];
	for(int i=0;i<=2;i++)
	{
		cin.getline(&line[i][0],80);
	}
	int length[10]={}; // 最长的单词长度为10,每个位置放入出现的次数
	for(int i=0;i<3;i++)
	{
		char *token=strtok(line[i]," ");
		while(token!=NULL)
		{
			length[strlen(token)]++;
			token=strtok(NULL," ");
		}
	}
	cout<<setw(4)<<"单词长度:"<<setw(4)<<"出现次数:"<<endl; 
	for(int i=0;i<10;i++)
	{
		cout<<setw(4)<<i<<":"<<setw(4)<<length[i]<<endl;
	}
	return 0;
}

在这里插入图片描述
PartC:
统计几行文本中的不同单词的出现次数,比如to 2次,be 2次

#include<iostream>
#include<iomanip>
#include<cstdio>
using namespace std;
int main()
{
	char line[2][80];
	for(int i=0;i<2;i++) // 输入line 
	{
		cin.getline(&line[i][0],80);
	}
	
	char words[80][10]={}; // 最大八十个单词,10个最长长度 
	int times[40]={};
	char s[]=", :\n";
	for(int i=0;i<2;i++)
	{
		char *token=strtok(&line[i][0],s);
		while(token!=NULL)
		{
			int j;
			for(j=0; words[j][0]&&strcmp(token,&words[j][0]);j++); 
			// strcmp相等则返回0,上面这个循环就是如果找到与之前相同的单词在words里的话,就停下,该位置++
			// 若没有找到相同的单词则意味着循环都全部结束,即为还没有放入word的位置,紧接着就放下新单词。 
			times[j]++; 
			if(!words[j][0])
			{
				strcpy(&words[j][0],token); // 将所判断的单词再次赋给那个words二维数组的对应值 
			}
			token=strtok(NULL,s); 
		}
	}
	cout<<endl;
	for(int k=0;words[k][0]!='\0';k++)
	{
		cout << "\"" << &words[ k ][ 0 ] << "\" appeared " << times[ k ]
         << " time(s)\n";
	}
	cout<<endl;
	return 0;
}

在这里插入图片描述
用cin.ignore() 也可以控制输入。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值