Thread: recursive string operation..

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    recursive string operation..

    I need to build a function which gets two strings checks if their chars are alpha type
    in ascending order.
    If they both are then it needs to merge them in a third d string.

    I cant call external functions.

    The whole process must be in one function
    and no iterative loops
    and only in a recursive way.

    I tried to build this function.

    i think it works
    but just after i input
    abc in the first string and def into the second one

    it does nothing and says that my program the EXE file
    encountered a problem
    and needs to be closed

    why??


    Code:
    #include <stdio.h>
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3);
    int main() {
        char input[255];
        char input2[255];
        char result[510];
        int index,flag,ch;
        printf("enter the first string \n");
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input[index] = ch;
        }
    
        input[index] = '\0';
    
        printf("enter the second string \n");
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input2[index] = ch;
        }
    
        input2[index] = '\0';
        flag=merge_strings(input, 0,input2,0, result, 0);
    
        if (flag)
        {
            printf("%s is valid.\n", result);
        }
        else
        {
            printf("input is invalid.\n");
        }
    
    return 0;
    }
    
    
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
     {
          int res,index;
          int res2;
          //start checking apha chars for string 1
         if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
         {
    
             return 1;
         }
         if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))
         {
    
             res=merge_strings(str1, index++,str2,index2, result, index3);
         }
         else
         {
           return 0;
         }
         //end checking apha chars for string 1
    
          //start checking apha chars for string 2
        if ((((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))&&(index==254))
           {
    
             return 1;
         }
         if (((str1[index1]>= 65) &&(str1[index1]<= 90))||((str1[index1]>= 97) &&(str1[index1]<= 122)))
         {
    
             res2=merge_strings(str1, index,str2,index2++, result, index3);
         }
         else
         {
           return 0;
         }
         //end checking apha chars for string 2
    
         if ((res==1)&&(res2==1))
         {
             result[index3]=str1[index];
             if(index<255)
             {
                 res=merge_strings(str1, index++,str2,index2, result, index3++);
             }
    
             result[index3]=str2[index2];
             if(index2<255)
             {
                 res=merge_strings(str1, index,str2,index2++, result, index3++);
             }
    
         }
         return 1;
    }
    Last edited by transgalactic2; 01-08-2009 at 02:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM