[ create a new paste ] login | about

Link: http://codepad.org/Htbky0ON    [ 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[]="Hello world a friend in the earth \n";
	char dst[] = "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<=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(strlen(str1)>strlen(str2)){
     for(int i=0;i<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<strlen(str)-1;i++)
    {
        str[i]=str[i+1];
    }
    return str;
}
char* removechar(char* str, char c)
{
    for(int i=0;i<strlen(str)-1;i++)
    {
        if(str[i]==c)
          str=remove_char(str,i+1);
    }
    return str;
}
/*char* remove_dup(char* str)
{ 
    int i=0,j=1;
    while(strlen(str)>0)
    {if(str[i]==str[j])
       {
           arr2[k]=str[i];
           str=removechar(str,str[i]);
           k++;
       }
    else
    {  
        j++;
        if(j==strlen(str))
          {
             arr2[k]=str[i]; 
             str=removechar(str,str[i]);
             k++;
             j=1;
          }
    }
    }
    return arr2;   
}*/
int sum_digit(char* str)
{
    int sum=0;
    int i=0;
 for(int i=0;i<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<=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;
    for(int i=0;i<strlen(str);i++)
      new[i]=str[strlen(str)-i-1];
    for(int j=0;j<strlen(str);j++)
      str[j]=new[j];
    return str;  
}
/*char* reverse(char* str)
{
    for(int i=0;i<strlen(str)/2;i++)
    {
        swap(str[i],str[strlen(str)-i-1]);
    }
    return str;
}*/
int char_in_str(char* str, char c)
{
    for(int i=0;i<strlen(str);i++)
    {
        if(str[i]==c)
           return i+1;
    }
}
int wrdcount(char* str)
{  
    int sum=0;
    for(int i=0;i<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<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<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;
}
}
//void print_words(char* str)
char* reverse_words(char* str)
{
    int i=0;
    while(i<strlen(str)){
      if(str[i]==' ')
         { i++;
           
        
}
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 132: error: 'for' loop initial declaration used outside C99 mode
In function 'remove_char':
Line 150: error: 'for' loop initial declaration used outside C99 mode
In function 'removechar':
Line 158: error: 'for' loop initial declaration used outside C99 mode
In function 'sum_digit':
Line 193: error: redefinition of 'i'
Line 192: error: previous definition of 'i' was here
Line 193: error: 'for' loop initial declaration used outside C99 mode
In function 'sum_decimal':
Line 202: error: 'for' loop initial declaration used outside C99 mode
In function 'reverse':
Line 223: error: 'for' loop initial declaration used outside C99 mode
Line 225: error: 'for' loop initial declaration used outside C99 mode
In function 'char_in_str':
Line 239: error: 'for' loop initial declaration used outside C99 mode
In function 'wrdcount':
Line 248: error: 'for' loop initial declaration used outside C99 mode
In function 'longest_word':
Line 264: error: 'for' loop initial declaration used outside C99 mode
Line 280: error: redefinition of 'i'
Line 264: error: previous definition of 'i' was here
Line 280: error: 'for' loop initial declaration used outside C99 mode
In function 'shortest_word':
Line 290: error: 'for' loop initial declaration used outside C99 mode
Line 307: error: redefinition of 'i'
Line 290: error: previous definition of 'i' was here
Line 307: error: 'for' loop initial declaration used outside C99 mode
In function 'reverse_words':
Line 336: error: expected declaration or statement at end of input
Line 336: error: expected declaration or statement at end of input


Create a new paste based on this one


Comments: