@[TOC]
100000580 《算法筆記》3.6小節(jié)——入門模擬->字符串處理
來(lái)自 http://codeup.cn/contest.php?page=6
講解
例題
Codeup 5901見(jiàn)習(xí)題
PAT B 1009 說(shuō)反話 (20 分)
來(lái)自
https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960
題析:主要是輸入時(shí)候用二維字符數(shù)組接收一行字符串,需要注意
PAT單點(diǎn)測(cè)試不需要用gets()
還有法二見(jiàn):
https://blog.csdn.net/xsj_blog/article/details/51992540
//1009說(shuō)反話
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[85][85];
int count=0;
while(scanf("%s",str[count]) != EOF)
{
count++;
}
for(int i=count-1;i>=0;i--)
{
printf("%s",str[i]);
if(i>0) printf(" ");//注意空格條件撼嗓,最后一個(gè)但此后面沒(méi)有空格,否則出錯(cuò)
}
printf("\n");
return 0;
}
/*
注意:在黑框中手動(dòng)輸入時(shí),系統(tǒng)并不知道什么時(shí)候到達(dá)了所謂的“文件末尾“,
因此需要用< Ctrl + Z >組合鍵碴倾,
然后按< Enter >鍵的方式來(lái)告訴系統(tǒng)已經(jīng)到了 EOF害淤,這樣系統(tǒng)才會(huì)結(jié)束 while
*/
練習(xí)題:
1785 Problem A 字符串連接
來(lái)自 http://codeup.cn/contest.php?cid=100000580
//1785ProblemA字符串連接
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str1[105];
char str2[105];
char str[315];
while(scanf("%s%s",str1, str2)!=EOF)
{
int len1=strlen(str1);
int len2=strlen(str2);
int count=0;
//略簡(jiǎn)潔
for(int i=0;i<len1+len2;i++)
{
if(i<len1)
str[i]=str1[i];
else
str[i]=str2[i-len1];
}
//略繁瑣
/*
for(int i=0;i<len1;i++)
{
str[count++] = str1[i];
}
for(int j=0;j<len2;j++)
{
str[count++] = str2[j];
}
for(int k=0;k<count;k++)
{
printf("%c",str[k]);
}
*/
str[len1+len2]='\0';//字符串結(jié)尾需加結(jié)束符號(hào),否則報(bào)錯(cuò)50%
printf("%s\n",str);
// printf("\n");
}
return 0;
}
1805 Problem B 首字母大寫
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:
通用解法金拒,多點(diǎn)測(cè)試用一維字符數(shù)組,然后轉(zhuǎn)存二位字符數(shù)組套腹,用row/column行列指針來(lái)控制下標(biāo)殖蚕,相應(yīng)改變字符串即可。
附大佬解法:
https://blog.csdn.net/qq_40073459/article/details/86559451
//1805ProblemB首字母大寫
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[105];
while(gets(str) != NULL)
{
int lens = strlen(str);
char string[105][105]={};//若不初始化為空則會(huì)影響到下一組測(cè)試
int row=0,column=0;
for(int i=0;i<lens;i++)//str轉(zhuǎn)移到二維字符數(shù)組string中
{
if(str[i] != ' '&&str[i]!='\t'&&str[i]!='\r'&&str[i]!='\n')
{
string[row][column++] = str[i];
}
else
{
string[row++][column]='\0';
column=0;
}
}
for(int i=0;i<=row;i++)
{
if(string[i][0]>='a' && string[i][0]<='z')
{
string[i][0] -= 32;//相差32
// string[i][0] = string[i][0]-'a'+'A'; //該語(yǔ)句導(dǎo)致錯(cuò)誤50%,雖然不知道為啥
}
}
for(int i=0;i<=row;i++)
{
if(i<row)
printf("%s ",string[i]);
else
printf("%s",string[i]);
}
printf("\n");
// getchar();
}
return 0;
}
1808 Problem C 字符串的查找刪除
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:題目有些繞沉迹,注意:
Gets()的用法
母串子串比較過(guò)程的模擬(特別是指針的動(dòng)態(tài)變化)睦疫,詳見(jiàn)注釋
//1808ProblemC字符串的查找刪除
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool compare(char a,char b)//將大小寫相同的情況都包括進(jìn)去的字符比較
{//簡(jiǎn)單粗暴的吧子串和母串的字符進(jìn)行比較,而不必改變各自的值
if(a>='A'&&a<='Z')
{
a=a-'A'+'a';
}
if(b>='A'&&b<='Z')
{
b=b-'A'+'a';
}
if(a==b)
return 1;
else
return 0;
}
char substr[1005];//子串字符數(shù)組鞭呕,注意申請(qǐng)足夠大
char str[1005];
int main()
{
int i,j;
gets(substr);
int sublen = strlen(substr);
/*//轉(zhuǎn)換為compare()函數(shù)更高效
for(i=0;i<sublen;i++)//待刪除字符串一律變成小寫蛤育,tolower
{
if(substr[i]>='A'&&substr[i]<='Z')
substr[i] = substr[i] - 'A' + 'a';
}
*/
while(gets(str) != NULL)//輸入母字符串
{
int len = strlen(str);
//字符串比較
i=0;j=0;
while(i<sublen&&j<len)
{
if(compare(substr[i],str[j]))//若當(dāng)前指針字符相等,則指針后移
{
i++;
j++;
//子串遍歷完葫松,說(shuō)明母串中有一個(gè)匹配的瓦糕,則除了移動(dòng)指針,子串指針應(yīng)重置
if(i==sublen)
i=0;
}
else//字符串不匹配腋么,則打印母字符串不匹配部分咕娄,并且指針重新計(jì)數(shù)
{
j = j-i+1;//不匹配的話,母串指針跳過(guò)匹配段向后
if(str[j-1] != ' ')//打印前一項(xiàng)
printf("%c",str[j-1]);
i=0;//子串指針重置
}
}
if(j==len) //一行結(jié)束換行
printf("\n");
}
return 0;
}
1962 Problem D 單詞替換
來(lái)自 http://codeup.cn/contest.php?cid=100000580
//1962ProblemD單詞替換
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[1010];
char str[105][105];
char a[105],b[105];
while(gets(s) != NULL)
{
int lens,lena,lenb;
int i=0,j,row=0,column=0;
lens=strlen(s);
for(i=0;i<=lens;i++)//將輸入母串轉(zhuǎn)換為二維數(shù)組存儲(chǔ)
{
if(s[i]!=' '||i==lens)
{
str[row][column++]=s[i];
}
if(s[i]==' ')
{
str[row++][column]='\0';
column=0;
}
}
// scanf("%s",a);
// scanf("%s",b);
gets(a);
gets(b);
for(i=0;i<=row;i++)
{
if(strcmp(str[i],a) == 0)//母串中匹配到子串a(chǎn)珊擂,用b替換
{
printf("%s",b);
// strcpy(str[i],b);
}
else
printf("%s",str[i]);
if(i<row)
printf(" ");
else
printf("\n");
}
}
getchar();
return 0;
}
1963 Problem E 字符串去特定字符
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:見(jiàn)解析
//1963ProblemE字符串去特定字符
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[100005],c;
// while(scanf("%s %c",s,&c))//單點(diǎn)圣勒,不合題意
while(gets(s) != NULL)//多點(diǎn)測(cè)試
{
c=getchar();//輸入 字符 兩種方法均可
//scanf("%c",&c);
int len = strlen(s);
for(int i=0;i<len;i++)
{
if(s[i]!=c)//不匹配到刪除字符則打印輸出
printf("%c",s[i]);
}
printf("\n");
getchar();//不加getchar()出錯(cuò)%50,摧扇?圣贸? 大概影響下一次gets(),畢竟是換行符
}
return 0;
}
1967 Problem F 數(shù)組逆置
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:真 水題
注意輸入的字符串可能會(huì)有空格 get() gets() 與 scanf()區(qū)別
//1967ProblemF數(shù)組逆置
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[205];
while(gets(str) != NULL)//注意輸入的字符串可能會(huì)有空格
{
int len = strlen(str);
for(int i=len-1;i>=0;i--)
{
printf("%c",str[i]);
}
printf("\n");
}
return 0;
}
2025 Problem G 比較字符串
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:簡(jiǎn)單題扛稽,注意cstring頭文件和strlen與char str[]的配合應(yīng)用
//2025ProblemG比較字符串
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str1[55],str2[55];
int len1,len2;
int m;
scanf("%d",&m);
while(m--)//多點(diǎn)測(cè)試
{
scanf("%s%s",str1,str2);
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 == len2)
{
printf("%s is equal long to %s\n",str1,str2);
}
else if(len1>len2)
{
printf("%s is longer than %s\n",str1,str2);
}
else
printf("%s is shorter than %s\n",str1,str2);
}
return 0;
}
2064 Problem H 編排字符串
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:
數(shù)組下標(biāo)的處理比較繁瑣吁峻,具體見(jiàn)注釋
將字符串計(jì)數(shù)與輸出指針?lè)指铋_(kāi)來(lái),并將字符數(shù)4作為分界條件
//2064ProblemH編排字符串
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int m;
scanf("%d",&m);
char str[4][25];
int i,j,count=0;
while(m--)//多點(diǎn)測(cè)試
{
scanf("%s",str[count%4]);//輸入字符串并存入數(shù)組在张,多出4個(gè)則覆蓋
i=count;//i用來(lái)控制輸出下標(biāo)
if(count<4)//字符串少于4個(gè)時(shí)用含,上限用count表示
{
for(j=1;j<=count+1;j++)
{
printf("%d=%s",j,str[i--]);
if(j==count+1)
printf("\n");
else
printf(" ");
}
}
else//字符串多于4個(gè)時(shí),上限不用count表示
{
for(j=1;j<5;j++)
{
printf("%d=%s",j,str[(i--)%4]);//注意取余
if(j==4)
printf("\n");
else
printf(" ");
}
}
count++;//輸入字符串計(jì)數(shù)
}
return 0;
}
5901 Problem I 【字符串】回文串
來(lái)自 http://codeup.cn/contest.php?cid=100000580
題析:很簡(jiǎn)單帮匾,兩頭往中間跑啄骇,但是scanf()輸入時(shí)輸出超限什么鬼?辟狈?已解決覺(jué)肠缔,沒(méi)加!=EOF,具體機(jī)制還是不清楚哼转?明未??
scanf 和 gets 讀取字符串
來(lái)自 http://www.cnblogs.com/qinjunni/archive/2012/03/03/2378323.html
//5901ProblemI【字符串】回文串
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool Judge(char str[])
{
int len = strlen(str);
for(int i=0;i<len/2;i++)
{
if(str[i]==str[len-i-1])
{
continue;
}
else
return 0;
}
return 1;
}
int main()
{
char str[260];
//while(scanf("%s",str))//輸出超限壹蔓?趟妥??佣蓉?
//while(gets(str))
while(scanf("%s",str)!=EOF)//已解決覺(jué)披摄,沒(méi)加!=EOF,具體機(jī)制還是不清楚勇凭?疚膊??
{
if(Judge(str))
{
printf("YES\n");
}
else
printf("NO\n");
// printf("\n");
}
return 0;
}