Moon Light Box

Time is Money

Posts match “ C ” tag:

C - Static Function

| Comments

A static function is a function whose scope is limited to the current source file.

For example : A static functionA is defined in 426.c and 689.c cannot call functionA.

C - Delete Space of String

| Comments

#include <stdio.h>
#include <stdlib.h>

void delete_space(char *s1, char *s2)
{
    while(*s1 != NULL)
    {
        if(!isspace(*s1))
        {
            *s2 = *s1;
            s2++;
        }
        s1++;
    }
    *s2 = '\0';
}

int main(int argc, char *argv[])
{
    char buf[80], buf1[80];

    int i;

    printf("Please input a string\n");

    gets(buf);

    delete_space(buf, buf1);

    printf("The result of delete space from string\n");

    puts(buf1);

    system("PAUSE");

    return 0;
}

C - Reverse String

| Comments

#include <stdio.h>
#include <string.h>

void reverse(char* str)
{
    int i, j;
    char c;
    for(i = 0, j = strlen(str)-1; i<j; ++i, --j)
        c = str[i], str[i] = str[j], str[j] = c;
}

int main()
{
    char str[] = "ABCDE";
    reverse(str);
    puts(str);
    return 0;
}

C - Get the Greatest Common Divisor

| Comments

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int a, b, temp;

    while(scanf("%d %d", &a, &b) == 2)
    {
        while(a%b)
        {
            temp = a;
            a = b;
            b = temp % b;
        }

        printf("%d\n",b);
    }
    system("PAUSE");
    return 0;
}