c++程序输入字符串数组,找到字符串最后一个字符 如何用c++定义一个数组,并在数组中查找指定的字符
\u7528C++\u8bed\u8a00\u5b9e\u73b0\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u5c06\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u7684\u4e2d\u95f4\u540e\u8f93\u51fa\u957f\u5ea6\u4e0d\u8d85\u8fc7100\uff0c\u6025\uff01\uff01\uff01\u4e00\u4e2a\u8981\u628a\u5b57\u7b26\u4e32\u5b9a\u4e49\u5b57\u7b26\u6570\u7ec4\uff0c\u4e0d\u5927\u4e8e100
Char str[100]
Len\u53d6\u51fa\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c
\u957f\u5ea6\u51cf1\u5c31\u662f\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u628a\u5b83\u7528temp\u5b58\u8d77\u6765\uff0c
\u7136\u540e\u7528\u5faa\u73af\u6765\u4ea4\u6362\u5b57\u7b26\uff0c\u5047\u5982\u7528for
For(i=len(str),i<len(str)/2,i--)
Temp=str[i-1];
Str[i-1]=str[i-2];
Str[i-2]=temp;
\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86
\u539f\u7406\u5c31\u662f\u4e0d\u65ad\u7684\u6570\u5b57\u4ea4\u6362\uff0c\u4ece\u6700\u540e\u4e00\u4f4d\u5f00\u59cb\uff0c\u4ea4\u6362\u5230\u4e2d\u95f4\u4e00\u4f4d\u5c31\u505c\u6b62\uff01
\u601d\u8def\uff1a\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u63a5\u7740\u8f93\u5165\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u8f93\u5165\u9700\u8981\u67e5\u627e\u7684\u5b57\u7b26\uff0c\u904d\u5386\u5b57\u7b26\u6570\u7ec4\uff0c\u67e5\u627e\u8f93\u5165\u7684\u5b57\u7b26\uff0c\u5982\u679c\u627e\u5230\u505c\u6b62\u904d\u5386\u8f93\u51fa\u4f4d\u7f6e\uff0c\u5982\u679c\u904d\u5386\u7ed3\u675f\u8fd8\u6ca1\u627e\u5230\uff0c\u8f93\u51fa\u6ca1\u6709\u627e\u5230\u3002
\u53c2\u8003\u4ee3\u7801\uff1a
//\u53c2\u8003\u4ee3\u7801 #includeusing namespace std;int main(){char ch[100],c;int i;cin>>ch;cin>>c;for(i=0;i<strlen(ch);i++)if(ch[i]==c){cout<<i;break;}if(i==strlen(ch))cout<<"\u6ca1\u6709\u627e\u5230\uff01"; return 0;}/*\u8fd0\u884c\u7ed3\u679c\uff1adsafwera2*/
#include <stdio.h>
#include <string.h>
void find(char *str,char *data)
{
int i=0;
int j=0;
int index;
int flag=0;
while(*(str+i)!='\0')
{
if(*(str+i)==*(data+j))
{
index=i;
i++,j++;
while(*(data+j)!='\0')
{
if(*(str+i)==*(data+j))
i++,j++;
else
break;
}
if(*(data+j)=='\0')
{
printf("%d ",index);
flag=1;
}
else
i=index+1;
}
else
{
i++;
}
j=0;
}
if(flag==0)
printf("No find!
");
else
printf("
");
}
int main()
{
char *str ="This is a test of c program!";
char data[80];
scanf("%s",data);
while(strcmp("###",data)!=0)
{
find(str,data);
scanf("%s",data);
}
return 0;
}
运行效果如下:
绛旓細include <string.h> void find(char *str,char *data){ int i=0;int j=0;int index;int flag=0;while(*(str+i)!='\0'){ if(*(str+i)==*(data+j)){ index=i;i++,j++;while(*(data+j)!='\0'){ if(*(str+i)==*(data+j))i++,j++;else break;} if(*(data+j)...
绛旓細ch = getchar(); // 璇诲叆涓涓瓧绗 int i;for (i = 0; i < strlen(str); i++) { // 閬嶅巻瀛楃鏁扮粍 if (str[i] == ch) { // 濡傛灉鎵惧埌浜嗘寚瀹氬瓧绗 printf("鎸囧畾瀛楃鍦ㄥ瓧绗︽暟缁勪腑绗竴娆″嚭鐜扮殑浣嶇疆锛%d\n", i); // 杈撳嚭涓嬫爣 return 0; // 绋嬪簭缁撴潫 } } // 濡傛灉娌℃湁鎵惧埌鎸...
绛旓細杈撳叆鏍蜂緥锛10 3 -5 2 8 0 3 5 -15 9 10011 6 4 8 2 6 -5 9 0 100 8 1杈撳嚭鏍蜂緥锛3 5 -15 6 4 1 鎴戠殑鎯虫硶鏄厛鎷緼鏁扮粍鍘昏窡B鏁扮粍姣斾竴閬嶏紝鐒跺悗鍐嶅弽杩囨潵姣斾竴閬嶏紝鎶婁笉鏄叡鏈夌殑鏁版斁鍒C鏁扮粍涓紝鏈鍚庢秷鍘鏁扮粍C涓浉鍚岀殑鏁拌緭鍑猴紝杩欐牱杈撳嚭鐨勫氨涓嶄細瀛樺湪鍚屼竴涓暟浜 ...
绛旓細C璇█绋嬪簭锛歩nclude <stdio.h>#include <string.h>#define MAX 80void main(){char arr[MAX + 1];char max;int len;int i;printf("Input a string:\n");gets(arr);max = '\0';len = strlen(arr);for(i=0; i<len; i++){if(arr[i] > max){max = arr[i];}}printf("The...
绛旓細绋嬪簭鏈夎锛屼慨鏀瑰涓嬶細include <stdio.h> include <string.h> void main(){ char sh[20];gets(sh);for(int i=0;i<20;i++){ if(sh[i]='@'){ printf("鍒ゆ柇鍑烘湁瀛楃@\n");break;} if(sh[i]='\0')printf("鏃犲瓧绗\n");} } ...
绛旓細include "stdio.h"#include "string.h"void main(){ char a[20],b[20]; int i,j=0; gets(a); for(i=0;i<strlen(a);i++) { if(a[i]%2!=0) b[j++]=a[i]; } b[j]='\0'; puts(b);} ...
绛旓細瀛楃涓閲屾壘涓涓瓧绗︼紝杈撳嚭鎵惧埌鐨勪釜鏁锛岀▼搴濡備笅锛歩nclude <stdio.h> int main(){ char s[100];char t;int i,n=0;printf("input one line string:\n");gets(s);printf("input a char which you want to search:\n");fflush(stdin); //涓轰簡璇诲叆涓涓瓧绗︼紝瑕佸厛娓呴櫎涓娆杈撳叆缂撳啿鍖 s...
绛旓細include<string> void main(){ char a[20],c; //a[20]涓哄瓧绗︿覆锛宑涓鸿鏌ユ壘瀛楃 int i,n[5],num = 0,j = 0; //i,j涓鸿鏁板櫒锛宯[5]绾綍涓嬫爣,num璁板綍涓暟 printf("璇杈撳叆瀛楃涓锛堝皯浜20涓瓧绗︼級\n");gets(a);printf("璇疯緭鍏ヨ鏌ユ壘瀛楃锛");scanf("%c",&c);for(i =...
绛旓細//濡傛灉鎵惧埌鍒欒繑鍥炲湪闀瀛楃涓涓殑璧峰浣嶇疆, 鑻ユ壘涓嶅埌鍒欒繑鍥-1//瑕佽繑鍥1鎴0, 鏀逛竴涓嬪氨鍙互浜唅nt find(char* source, char* target){int i,j;int s_len=strlen(source);int t_len=strlen(target);if(t_len>s_len){return -1;}for(i=0;i<=s_len-t_len;i++){j=0;int flag=1;...
绛旓細flag;//杈撳叆涓涓插瓧绗printf("Input a string:");gets(s);//杈撳叆鎯宠鏌ユ壘鐨勫瓧绗rintf("Input another string:");gets(d);//璋冪敤鍑芥暟锛岃緭鍑虹粨鏋渇lag=SearchString(s,d);if(flag==-1)printf("Not found!\n");elseprintf("Searching results:%d\n",flag);system("pause");...