---
# System prepended metadata

title: 1B-10
tags: [一上程設]

---

# 1B-10
###### tags: `一上程設`
### eg1
```
#include <stdio.h>
int sum(int list[], int size)
{
    int i, sum = 0;
    for(i = 0; i < size; i++)
        sum += list[i];

    return sum;
}
int main(void)
{
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8 ,9 , 10};
    int s;
    int *p = a;
    s = sum(a, 10);
    printf("The sum is %d\n", s);
    s = sum(&a[0], 8); //sum(p, 5)
    printf("The sum is %d\n", s);
    s = sum(&a[2], 4); //sum(p+2, 4)
    printf("The sum is %d\n", s);
    return 0;

}

```
### eg3
```
#include <stdio.h>
int sum_variant_1(int *, int);
int sum_variant_2(int *, int);
int sum_variant_3(int *, int);
int sum_variant_4(int *, int);

int main(void)
{
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8 ,9 , 10};
    int s;
    int *p = a;
    s = sum_variant_1(p, 10);
    printf("The sum is %d\n", s);
    s = sum_variant_2(p, 10);
    printf("The sum is %d\n", s);
    s = sum_variant_3(p, 10);
    printf("The sum is %d\n", s);
    s = sum_variant_4(p, 10);
    printf("The sum is %d\n", s);
    return 0;

}
int sum_variant_1(int *ptr, int size)
{
    int i, sum = 0;
    for(i = 0; i < size; i++)
        sum += *++ptr;

    return sum;
}
int sum_variant_2(int *ptr, int size)
{
    int i, sum = 0;
    for(i = 0; i < size; i++)
        sum += (*ptr)++;

    return sum;
}
int sum_variant_3(int *ptr, int size)
{
    int i, sum = 0;
    for(i = 0; i < size; i++)
        sum += ++*ptr;

    return sum;
}
int sum_variant_4(int *ptr, int size)
{
    int i, sum = 0;
    for(i = 0; i < size; i++)
        sum += --*ptr++;

    return sum;
}

```
### eg4.1
```
#include <stdio.h>

void showString(char *);

int main(void)
{
    char str[5];

    showString(fgets(str, 5, stdin));
    printf("The string you just enter is:\n%s\n", str);
    return 0;
}

void showString(char *str)
{
    for(; *str != '\0';)
    {
        printf("%c\t%d\n", *str, *str);
        str++;
    }
}


```
### eg4.2
```
#include <stdio.h>
#define N 20

void denewline(char *);

int main(void)
{
    char str[N];
    fgets(str, 20, stdin);
    denewline(str);
    printf("The string is:\n%s\n", str);
    return 0;
}

void denewline(char *s)
{
    for(; *s != '\0'; s++)
    {
        if(*s ==  '\n')
            *s = '\0';
    }
}

```
### eg4.3
```
#include <stdio.h>
#include <string.h>
#define N 20
void despace(char *);
int main(void)
{
    char str[N];

    fgets(str, 20, stdin);
    despace(str);
    return 0;
}
void despace(char *s)
{
    int Space = 0;
    for(; *s == '\0'; s++)
    {
        if(*s == ' ')
            Space++;
    }
    memmove(s, s + Space, strlen(s) - Space);
    printf("%s", s);
}



```