100个字符的字符串,选择其中带有qwe字符开头的20个字符,请用C语言写出
时间: 2024-05-19 20:17:22 浏览: 74
输入20个字符串并排序
5星 · 资源好评率100%
char* selectString(char* str) {
char* selected = (char*)malloc(21*sizeof(char)); // allocate memory for selected string
int count = 0;
while (*str && count < 20) { // loop through the entire string
if (*str == 'q' && *(str+1) == 'w' && *(str+2) == 'e') { // check if the string starts with 'qwe'
for (int i = 0; i < 20; i++) {
*(selected+i) = *(str+i); // if so, copy the next 20 characters to selected string
}
*(selected+20) = '\0'; // add null terminator to the end
break; // exit loop
}
count++; // increase count for every character processed
str++; // move pointer to next character
}
return selected;
}
// call the function with a sample string
int main() {
char* str = "qwertyuiopasdfghjklzxcvbnmqwertyui";
char* selected = selectString(str);
printf("%s\n", selected); // output: "qwe"
free(selected); // free allocated memory
return 0;
}
阅读全文