#include <stdio.h>
#include <string.h>
int main(){
char str[8] = "RREADFZ";
char str1[8] = {0};
int i, k, aPos, zPos, len = strlen(str);
aPos = zPos = -1;
for(i =0; i<len; i++){
if(str[i] == 'A'){
aPos = i;
} else if(str[i] == 'Z'){
zPos = i;
break;
}
}
if(aPos != -1 && zPos != -1){
for(k=0, i=zPos-1;i>aPos;--i,++k){
str1[k]=str[i];
}
}
printf("%s\n", str1);//FD
return 0;
}
UPDATE(定義函數)
#include <stdio.h>
#include <string.h>
void between(char out[], const char in[], char front, char back){
int i, k, frontPos, backPos, len = strlen(in);
frontPos = backPos = -1;
for(i =0; i<len; i++){
if(in[i] == front){
frontPos = i;
} else if(in[i] == back){
backPos = i;
break;
}
}
k = 0;
if(frontPos != -1 && backPos != -1){
for(i=backPos-1;i>frontPos;--i,++k){
out[k]=in[i];
}
}
out[k]='\0';
}
int main(){
char str[] = "RREADFZ";
char exp[] = "2-3 +5 * 7";
char cutout[16];
between(cutout, str, 'A', 'Z');
printf("str:%s\n", cutout);//FD
between(cutout, exp, '+', '*');
printf("exp:%s\n", cutout);// 5
return 0;
}
你想_decrement_'k'或_increment_呢? – devnull
你是否忘記了字符串末尾的空字符? –
@devnull我不會減少k – user3313154