---
# System prepended metadata

title: 期末上機考古題
tags: [一上程設]

---

# 期末上機考古題
###### tags: `一上程設`
### 1.
```
#include <stdio.h>
void replaceChar(char *, char, char);
int main(void)
{
    int N = 100;
    char str[N], ori, after;
    printf("Input a string:\n");
    fgets(str, N, stdin);
    printf("String entered is:\n%s\n", str);
    printf("Enter the character to be replaced and the new one: ");
    scanf("%c %c", &ori, &after);
    printf("String after replacement:\n");
    replaceChar(str, ori, after);
    printf("%s", str);
    return 0;
}
void replaceChar(char *s, char a, char b)
{
    for(; *s != '\0'; s++)
    {
        if(*s == a)
            *s = b;
    }
}
```
### 2.
```
#include <stdio.h>
#include <string.h>
#define N 100
void delFirstChars(char *, int);
int main(void)
{
    int del;
    char str[N];
    printf("Input a string:\n");
    fgets(str, N, stdin);
    printf("String enteredd is:\n");
    puts(str);
    printf("How many leading characters to delete:");
    scanf("%d", &del);
    delFirstChars(str, del);


    return 0;
}
void delFirstChars(char *s, int del)
{
    memmove(s, s + del, N);
    printf("%s", s);
}

```
### 3.
```
#include <stdio.h>
#define N 5
void initArray(int *);
void showArray(int *);
int main(void)
{
    int a[N][N];
    initArray(a);
    showArray(a);


    return 0;
}

void initArray(int *a)
{
    int i, j;
    for(i = 0; i < N; i++)
    {
        for(j =0; j < N; j++)
        {
            *a++=i+1;
        }
    }
}

void showArray(int *a)
{
    int i, j;
    for(i = 0; i < N; i++)
    {
        for(j =0; j < N; j++)
        {
            printf("%d", *a++);
        }
        printf("\n");
    }
}

```