Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by kott ( 16 years ago )
#include<stdio.h>
#include<stdlib.h>
#include<string.h>



char **stack;
int    top=-1;
int    stackSize=0;

void Error(char *str)
  {
     printf("Error: %s\n",str);
     exit(1);
  };

void *GetMemory(int bytes)
  {
     void *ptr;
     
     if((ptr = (void *)malloc(bytes)) == NULL)
        Error(" Oops can't malloc memory. ");
       
  return ptr;
  };

void CreateStack(int stackSize_)
     {
          stack = (char **)malloc(sizeof(char *)*stackSize_);
          top = stackSize = stackSize_;              
     };

void Push(char *string)
      {
           
        if(top > 0)
            stack[--top]=string;     
                 
      };
      
char *Pop()
      {
           
         if(stackSize > top)
            return stack[top++];
         
         return NULL;       
      };

int CountSimpleStrings(char *str)
  {
    char *ch;
    char *end=str+strlen(str);
    int countSimpleStrings=0;
    int beforeAlpha = 1;  
    
    ch = str;
    while(ch!=end && ( *ch==',' || *ch==' ') )
       ch++;
    
    while(end!=ch && ( *end==',' || *end==' '))
      end--;
    
    if(ch==end) return 0;
    
    for(ch; ch!=end; ++ch)
     {
      if(*ch==',')
       {
        if(beforeAlpha)
          { 
           countSimpleStrings++;
           beforeAlpha=0;
          };
       } 
       else
        if(*ch!=' ')beforeAlpha = 1;                
     };  
     
     
      
    return countSimpleStrings+1;            
  };
  
int CountWords(char *str)
  {
    char *ch=str;
    int countWords=0;
    int lastCharIsSpace = (*ch++ != ' ')? 0: 1;
     
     while(*ch)
      {
        if(*ch++==' ' )
         {
           if(!lastCharIsSpace)
             {
               countWords++;
               lastCharIsSpace = 1;
             }; 
          } else lastCharIsSpace = 0;        
      };
      
      if(!lastCharIsSpace)
        countWords++;
     
    return countWords;                
  };

char *ReverseSimpleString(const char *str)
  {
     char *word,*ch;
     char *resultString;
     char *curString;
     int lenght=1;
     
     curString = (char *)GetMemory(strlen(str)+1);
     strcpy(curString,str);
     
     CreateStack(CountWords(curString));
     
     word = strtok(curString," ");
     while(word)
      {
         lenght += strlen(word) + 1;        
         Push(word);
         word = strtok(NULL," ");        
      }; 
      
      ch = resultString = (char *)GetMemory(lenght);
      
      while( (word = Pop()))
       {
         strncpy(ch,word,strlen(word));
         ch += strlen(word); *ch++=' ';        
       };
       
       *(ch-1)=0;
       
    free(curString);
       free(stack);
       
       
       return resultString;                         
  };
      

char *ReverseString(char *str)
  {
     char **simpleStrings;
     char  *curString, *string;
     char *resultString=NULL;
     int lenght=1;
     int countString=0;
     int i=0;
     
     string = (char *)GetMemory(strlen(str)+1);
     strcpy(string,str);
    

     if((simpleStrings = (char **)malloc(sizeof(char *)*CountSimpleStrings(string)))==NULL)
   Error("Ooops can't malloc memory");
     
     curString = strtok(string,",");
    
     while(curString)
      {
        simpleStrings[countString++]=curString;
        lenght += strlen(curString); 
        curString = strtok(NULL,",");             
      };      
      
      curString = resultString = (char *)GetMemory(lenght);
      
      for(i=0; i < countString; ++i)
       {
      simpleStrings[i]=ReverseSimpleString(simpleStrings[i]);
         strncpy(curString,simpleStrings[i],strlen(simpleStrings[i]));
         curString+=strlen(simpleStrings[i]);  *curString++=','; *curString++=' ';
         free(simpleStrings[i]);
       }; 
       
       *(curString-2)=0;
       
       free(simpleStrings);
       free(string);
       
      return resultString;                          
  };
      

int main(int argc,char **argv)
  {
     char buffer[256];
     char *reversed;

    if(!fgets(buffer,256,stdin))
     Error("Ooops error read ;P ");
      
    buffer[strlen(buffer)-1]=0;

    reversed = ReverseString(buffer);

    printf("Read: %s\nReversed: %s\n",buffer,reversed);
          
     return 0;        
  };

 

Revise this Paste

Your Name: Code Language: