1.编写字符串查找函数mystrchr()。该函数的功能是在字符串中查找指字符,如果有该字符,则返回该字符在字符串中第一次出现的位置,则返回0,然后编写主函数进行验证。函数原形为: int mystrchr( char string[], char c);写出完整C程序; 2.编一程序,将字符串“He11o,C++!”赋给一个字符数组,然后从第一个字母开始间隔地输出该串(请用指针完成)。写出完整C程序。 3.1)建立职工数据结构,结构含有姓名、序号、性别和年龄信息; 2)建立一个结构数组,以便放置10个职工信息; 3)设计一个输入函数,输入职工的信息; 4)编写一个显示输出函数,用来输出结构数组的内容; 写出完整C程序。 结构数组
时间: 2023-04-04 12:02:28 浏览: 144
1.
```
#include <stdio.h>
int mystrchr(char string[], char c) {
int i;
for (i = 0; string[i] != '\0'; i++) {
if (string[i] == c) {
return i + 1;
}
}
return 0;
}
int main() {
char string[] = "Hello, C++!";
char c = 'l';
int pos = mystrchr(string, c);
if (pos) {
printf("'%c' found at position %d\n", c, pos);
} else {
printf("'%c' not found\n", c);
}
return 0;
}
```
2.
```
#include <stdio.h>
int main() {
char string[] = "Hello, C++!";
char *p = string;
while (*p != '\0') {
printf("%c", *p);
p += 2;
}
printf("\n");
return 0;
}
```
3.
```
#include <stdio.h>
struct Employee {
char name[20];
int id;
char gender;
int age;
};
void inputEmployee(struct Employee *e) {
printf("Enter name: ");
scanf("%s", e->name);
printf("Enter ID: ");
scanf("%d", &e->id);
printf("Enter gender (M/F): ");
scanf(" %c", &e->gender);
printf("Enter age: ");
scanf("%d", &e->age);
}
void outputEmployee(struct Employee *e) {
printf("Name: %s\n", e->name);
printf("ID: %d\n", e->id);
printf("Gender: %c\n", e->gender);
printf("Age: %d\n", e->age);
}
int main() {
struct Employee employees[10];
int i;
for (i = 0; i < 10; i++) {
printf("Enter information for employee %d:\n", i + 1);
inputEmployee(&employees[i]);
}
printf("Employee information:\n");
for (i = 0; i < 10; i++) {
outputEmployee(&employees[i]);
}
return 0;
}
```
阅读全文