[ create a new paste ] login | about

Link: http://codepad.org/GJ2DkjIK    [ raw code | output | fork ]

3107141220 - C, pasted on Oct 31:
#include <stdio.h>
#include<string.h>
int str_length(char* str);
int numcount(char* str);

char* str_copy(char* dst, char* str); // DONE
char* str_cat(char* str1, char* str2); // "abc" -> "Hello world \ n abc \0" DONE
int str_compare(char* str1, char* str2); // str1[i]-str2[i] != 0 DONE
char* str_str(char* str1, char* str2); // "world" -> str+i DONE

char* remove_char(char* str, int pos); //4, "Helo world \n" DONE
char* removechar(char* str, char c);// 'o' "Hell wrld \n" DONE
char* remove_dup(char* str); // "abca \n dbceedb \n" -> "abc \n de" " \n abcde" DONE

int sum_digit(char* str); // "1234" -> 10 DONE
int sum_decimal(char* str); // "123 45" -> 168 DONE
// res=res*10+str[i]-'0'

void swap(char** a, char** b); //DONE
char* reverse(char* str); // "abc \n \0"-> "\n cba \0" DONE
//char* reverse_sub(char* str, char* sub); //"world" -> "Hello dlrow \n" DONE

int char_in_str(char* str, char c); // 'o'-> 4 DONE

int wrdcount(char* str); //"Hello world abc" -> 3 DONE
int longest_word(char* str); //"Hello world " -> 5 DONE
int shortest_word(char* str); //"Hello world abc" -> 3 DONE
void print_words(char* str); //"Hello world " -> Hello / world DONE
char* reverse_words(char* str); // "Hello world " -> "olleH dlrow" DONE
char* reverse_sub(char* str, int start, int end); // " Hello worldab", start w(6),end l(10) -> Hello dlrowab DONE
char arr[100];
char arr2[100];
int main()
{
    char str[100] = "Hello world a friend in the earth \n";
    char dst[100] = "earth";
    printf("1 %s 2 %s 3 %d", str_copy(dst, str), str_cat(str, str), str_compare(str, str));
    char haha[] = "Hello my friend ";
    char hihi[] = "my";
    printf("\n4 %s", str_str(hihi, haha));
    int pos = 4;
    char c = 'o';
    printf("\n5 %s", remove_char(haha, pos));
    printf("\n6 %s", removechar(haha, c));
    char arr[] = "abca \n dbceedb \n";
    //printf("\n7 %s",remove_dup(arr));
    char huhu[] = "144 2367 n47488";
    printf("7 %d 8 %d", sum_digit(huhu), sum_decimal(huhu));
    char* a = "hihi";
    char* b = "haha";
    swap(&a, &b);
    printf("\n9 %s %s", a, b);
    //  reverse(a);
    char str1[10] = "Vlstudy";
    printf("\n10 %s", reverse(str1));
    char g[] = " hai phong";
    char gg = 'o';
    printf("\n 11 %d", char_in_str(g, gg));
    printf("\n 12 %d", wrdcount(" gag  ga g a g ag gjja g ga g ag a g"));
    printf("\n 13 %d 14 %d", longest_word(" Hello world78 a friend in the earth \n"), shortest_word(" Hello world78 afg friend in6 the earth \n"));
    char str90[20] = "Hello worldab";
    int start = 6, end = 10;
    printf("\n 15 %s", reverse_sub(str90, start, end));
    return 0;
}
int str_length(char* str)
{
    int i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return (i);
}
int numcount(char* str)
{
    int c = 0;
    int i = 0;
    while (str[i] != '\0')
    {
        if (str[i] >= '0' && str[i] <= '9')
        {
            c++;
        }
        i++;
    }
    return c;
}
char* str_copy(char* dst, char* str)
{
    int a = str_length(str);
    for (int i = 0; i <= a; i++)
    {
        dst[i] = str[i];
    }
    return dst;
}
char* str_cat(char* str1, char* str2)
{
    int a = str_length(str1);
    int b = str_length(str2);
    for (int i = 0; i < a + b; i++)
    {
        if (i < a)
            arr[i] = str1[i];
        else
            arr[i] = str2[i - a];
    }
    return arr;
}
int str_compare(char* str1, char* str2)
{
    int i, ok = 0;
    for (i = 0; i <= (int)strlen(str1); i++) {

        if (str1[i] != str2[i]) {

            if (str1[i] > str2[i])
            {
                ok = 1;
                return ok;
            }
            else
            {
                ok = 2;
                return ok;
            }
        }
    }
    if (ok == 0) return 0;
}
char* str_str(char* str1, char* str2)
{
    int j = 0;
    if ((int)strlen(str1) > (int) strlen(str2)) {
        for (int i = 0; i < (int)strlen(str1); i++)
        {
            if (str1[i] == str2[j])
            {
                i++;
                j++;
                if (str2[j] != str1[i]) j = 0;
            }
            else i++;
            if (j == strlen(str2) - 1)
                return str2;
        }
        if (j != strlen(str2) - 1)
            return "0";
    }
    else
        str_str(str2, str1);

}
char* remove_char(char* str, int pos)
{
    for (int i = pos - 1; i < (int)strlen(str) - 1; i++)
    {
        str[i] = str[i + 1];
        str[strlen(str)] = '\0';
    }
    return str;
}
char* removechar(char* str, char c)
{
    for (int i = 0; i < (int)strlen(str) - 1; i++)
    {
        if (str[i] == c)
            str = remove_char(str, i + 1);
    }
    return str;
}
int sum_digit(char* str)
{
    int sum = 0;
    int i = 0;
    for (int i = 0; i < (int)strlen(str); i++)
    {
        if (str[i] >= '0' && str[i] <= '9')
            sum += (int)str[i] - 48;
    }
    return sum;
}
int sum_decimal(char* str)
{
    int sum = 0, sum2 = 0;
    for (int i = 0; i <= (int)strlen(str); i++)
    {
        if (str[i] >= '0' && str[i] <= '9')
        {
            sum = sum * 10 + (int)str[i] - 48;
        }
        else {
            sum2 += sum;
            sum = 0;
        }
    }
    return sum2;
}
void swap(char** a, char** b)
{
    char* temp = *a;
    *a = *b;
    *b = temp;
}
char* reverse(char* str) {
    char new[100];
    for (int i = 0; i <(int) strlen(str); i++)
        new[i] = str[strlen(str) - i - 1];
    for (int j = 0; j < (int)strlen(str); j++)
        str[j] = new[j];
    return str;
}
int char_in_str(char* str, char c)
{
    for (int i = 0; i < (int)strlen(str); i++)
    {
        if (str[i] == c)
            return i + 1;
    }
}
int wrdcount(char* str)
{
    int sum = 0;
    for (int i = 0; i < (int)strlen(str); i++)
    {
        if (str[i] == ' ')
            i++;
        else
        {
            sum += 1;
            while (str[i] != ' ')
                i++;
        }
    }
    return sum;
}
int longest_word(char* str)
{
    int a[100]; int k = 0;
    int sum = 0;
    for (int i = 0; i < (int)strlen(str); i++)
    {
        if (str[i] == ' ')
            i++;
        else
        {
            while (str[i] != ' ')
            {
                i++;
                sum += 1;
                if (str[i] == ' ')
                    k++;
            }
            a[k] = sum;
            sum = 0;
        }
    }
    int c = a[0];
    for (int i = 0; i <= k; i++)
    {
        if (a[i] > c)
            c = a[i];
    }
    return c;
}
int shortest_word(char* str)
{
    {
        int a[100]; int k = 0;
        int sum = 0;
        for (int i = 0; i < (int)strlen(str); i++)
        {
            if (str[i] == ' ')
                i++;
            else
            {
                while (str[i] != ' ')
                {
                    i++;
                    sum += 1;
                    if (str[i] == ' ')
                        k++;
                }
                a[k] = sum;
                sum = 0;
            }
        }
        int c = a[1];
        for (int i = 0; i <= k; i++)
        {
            if (a[i] < c && a[i]>0)
            {
                c = a[i];
            }
        }
        return c;
    }
}
char* reverse_sub(char* str, int start, int end)
{
    char temp = *str;
    while (start < end) {
        temp = *(str + start);
        *(str + start++) = *(str + end);
        *(str + end--) = temp;
    }
    return(str);
}


Output:
In function 'str_copy':
Line 92: error: 'for' loop initial declaration used outside C99 mode
In function 'str_cat':
Line 102: error: 'for' loop initial declaration used outside C99 mode
In function 'str_str':
Line 136: error: 'for' loop initial declaration used outside C99 mode
In function 'remove_char':
Line 157: error: 'for' loop initial declaration used outside C99 mode
In function 'removechar':
Line 166: error: 'for' loop initial declaration used outside C99 mode
In function 'sum_digit':
Line 177: error: redefinition of 'i'
Line 176: error: previous definition of 'i' was here
Line 177: error: 'for' loop initial declaration used outside C99 mode
In function 'sum_decimal':
Line 187: error: 'for' loop initial declaration used outside C99 mode
In function 'reverse':
Line 208: error: 'for' loop initial declaration used outside C99 mode
Line 210: error: 'for' loop initial declaration used outside C99 mode
In function 'char_in_str':
Line 216: error: 'for' loop initial declaration used outside C99 mode
In function 'wrdcount':
Line 225: error: 'for' loop initial declaration used outside C99 mode
In function 'longest_word':
Line 242: error: 'for' loop initial declaration used outside C99 mode
Line 260: error: redefinition of 'i'
Line 242: error: previous definition of 'i' was here
Line 260: error: 'for' loop initial declaration used outside C99 mode
In function 'shortest_word':
Line 272: error: 'for' loop initial declaration used outside C99 mode
Line 290: error: redefinition of 'i'
Line 272: error: previous definition of 'i' was here
Line 290: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: