Showing posts with label Recursive. Show all posts
Showing posts with label Recursive. Show all posts

C Program Recursive

How to write a C Program Recursive in C Programming Language ?


Solution For C Program :

C Program to Check Prime Use Loop And Recursive

How to write a C Program to Check Prime Use Loop And Recursive in C Programming Language ?


Solution For C Program :
/*Check Prime Use Loop And Recursive*/

C Program to Recursive Procedure for Tree Traversals

How to write a C Program to Recursive Procedure for Tree Traversals in C Programming Language ?


Type Declaration of Tree Node


struct node
{
   struct treenode *lchild;
   char data;
   struct treenode *rchild;
};

Procedure for Recursive Preorder


void rpreorder(struct node *p)
{
   if (p!=NULL)
   {
      printf("%c ",p->data);
      rpreorder(p->lchild);
      rpreorder(p->rchild);
   }
}

Procedure for Recursive Inorder


void rinorder(struct treenode *p)
{
   if (p!=NULL)
   {
      rinorder(p->lchild);
      printf("%c ",p->data);
      rinorder(p->rchild);
   }
}

Procedure for Recursive Postorder


void rpostorder(struct node *p)
{
   if (p!=NULL)
   {
      rpostorder(p->lchild);
      rpostorder(p->rchild);
      printf("%c ",p->data);
   }
}

C Program to Recursively converts all file-names to lower-case

How to write a C Program to Recursively converts all file-names to lower-case in C Programming Language ?


Solution:


/*Recursively converts all file-names to lower-case.*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

int verbose = 0;

int convertDirectory ( const char *name )
    /* returns number of converted file-names, -1 if failed */
{
    DIR *dir;
    char fn[2048], oldFn[2048], *ptr;
    int len, rw, counter, needs, subCounter;
    struct dirent *entry;
    struct stat st;

    if ( !name || !name[0] ) return -1;
    dir = opendir(name);
    if ( !dir ) return -1;
    strcpy(fn,name);
    len = strlen(fn);
    fn[len++] = '/';
    rw = 0;                                 /* the directory is writtable? */
    counter = 0;                            /* number of changed file-names */
    while ( (entry = readdir(dir)) ) {      /* process one directory entry */
        if ( entry->d_name[0] == '.' &&
             (!entry->d_name[1] ||
              entry->d_name[1] == '.' &&
              !entry->d_name[2]) )
            continue;                       /* special directory */
            /* assemble the item name: */
        strcpy(fn+len,entry->d_name);
            /* lower-case conversion: */
        needs = 0;
        for ( ptr = fn + len; *ptr; ptr++ )
            if ( isupper(*ptr) ) {
                needs = 1;
                break;
                }
            /* the directory-entry needs to be converted: */
        if ( needs ) {
            if ( !rw ) {                    /* make the mother directory writable */
                fn[len-1] = (char)0;
                if ( !stat(fn,&st) && !(st.st_mode & S_IWUSR) )
                    chmod(fn,st.st_mode|S_IWUSR);
                fn[len-1] = '/';
                rw = 1;
                }
            strcpy(oldFn,fn);
            for ( ptr = fn + len; *ptr; ptr++ )
                if ( isupper(*ptr) )
                    *ptr = tolower(*ptr);
            rename(oldFn,fn);
            if ( verbose ) printf("%s -> %s\n",oldFn,fn);
            counter++;
            }
            /* stat the entry .. subdirectories must be traversed */
        if ( !stat(fn,&st) &&
             S_ISDIR(st.st_mode) ) {        /* directory */
            subCounter = convertDirectory(fn);
            if ( subCounter >= 0 )
                counter += subCounter;
            else
                if ( verbose )
                    printf("Error converting '%s' directory\n",fn);
            }
        }
    closedir(dir);

    return counter;
}

int main ( int argc, char *argv[] )
{
    /* TODO: command-line argument: directory to convert */
    /* TODO: -v (verbose) */
    int converted = convertDirectory(".");
    if ( converted <= 0 )
        printf("No file-names were converted\n");
    else
        printf("%d file-names were converted\n",converted);
    return 0;
}

C Program Recursive function that searches for a given file in a given folder

How to write a C Program to Recursive function that searches for a given file in a given folder in C Programming Language ?


Input: 
  1. Char pointer to the path folder.
  1. Char pointer to the target file name.
  1. Char pointer to the type of file to search for.
Solution:


/*
 * Recursive function that searches for a given file in a given folder.
 * input: Char pointer to the path folder.
 *        Char pointer to the target file name.
 *        Char pointer to the type of file to search for.
 */
void find_file(char* path, char* target, char typeValue){
    DIR *dir;
    struct dirent *entry;

    if (!(dir = opendir(path))){
        perror("Could not open directory.");
        return;
    }
    
    while ((entry = readdir( dir )) != NULL) {
        if ( !strcmp( entry->d_name, "."  )) continue;
        if ( !strcmp( entry->d_name, ".." )) continue;

        struct stat s;
        char fullpath[strlen(path) + strlen(entry->d_name) + 2];
        sprintf(fullpath, "%s/%s", path, entry->d_name);
        lstat(fullpath, &s);

        // If entry is a directory - add directory to list
        if (S_ISDIR(s.st_mode)) {
            addFolderToList(fullpath);
        }

        // Switch over type value, only print matches with correct type
        if (strcmp(target, entry->d_name) == 0) {
            switch (typeValue){
            case ('a'):
                    printf("%s \n", fullpath);
                    break;
            case ('d'):
                if (S_ISDIR(s.st_mode)) printf("%s \n", fullpath);
                break;
            case ('l'):
                if(S_ISLNK(s.st_mode)) printf("%s \n", fullpath);
                break;
            case ('f'):
                if(S_ISREG(s.st_mode)) printf("%s \n", fullpath);
                break;
            }
        }
    }
    closedir(dir);
}