c++编写一个程序,有一个字符串"I LOVE SAI C++ IS MY Favorite --SAI-C++ ",请查找第一次和最后一次出现S的位置,在字符串前面加入自己的姓名,专业
时间: 2024-05-23 14:14:42 浏览: 93
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "I LOVE SAI C IS MY Favorite --SAI-C ";
char name[] = "AI Assistant";
char major[] = "Computer Science";
char *p;
// 查找第一次出现S的位置
p = strchr(str, 'S');
printf("%s %s: ", name, major);
if(p){
printf("The first occurrence of S is at position %ld.\n", p - str);
}
else{
printf("S is not found.\n");
}
// 查找最后一次出现S的位置
p = strrchr(str, 'S');
printf("%s %s: ", name, major);
if(p){
printf("The last occurrence of S is at position %ld.\n", p - str);
}
else{
printf("S is not found.\n");
}
return 0;
}
阅读全文