Feb 3, 2012

K & R C Programs Exercise 6-2.

K and R C, Solution to Exercise 6-2:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be from the command line. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

struct tnode {
 char *word;
 int match;
 struct tnode *left;
 struct tnode *right;
};
#define MAXWORD
#define YES 0
#define NO 0

struct tnode *addtreex(struct tnode *, char *,int, int);
void treexprint(struct tnode *);
int getword(char *,int);


main(int argc, char argv[])
{
 struct tnode  *root;
 char word[MAXWORD];
 int found = NO;
 int num;

 num = (--argc && (*++argv)[0] == '-') ? atoi(argv[0] +1) : 6;
 root = NULL;
 while(getword(word, MAXWORD) != EOF) {
  if(isalpha(word[0]) && strlen(word) >= num)
   root = addtreex(root, word, num, &found);
  found = NO;
 }
 treexprint(root);
 return 0;
}
struct tnode *talloc(void);
int compare(char *,struct tnode *, int, int *);
//addtreex: add a node with w, at or below p
struct tnode *addtreex(struct tnode *p, char *w,int num, int *found)
{
 int cond;
 if(p == NULL) {
  p = talloc();
  p->word = strdup(w);
  p->match = *found;
  p->left = p->right = NULL;
 }
 else if((cond = compare(w,p,num,found)) < 0)
  p->left = addtreex(p->left, w, num, found);
 else if(cond > 0)
  p->right = addtreex(p->right, w, num, found);
 return p;
}

//compare:compare words and update p->match
int compare(char *s,struct tnode *p, int num, int *found)
{
 int i;
 char *t = p->word;
 for(i = 0; *s == *t;i++,s++,t++)
  if(*s == '\0')
   return 0;
 if(i >= num) {
  *found = YES;
  p->match = YES;
 }
 return *s - *t;
}

/*treexprint: in-order print of tree p if p->match == YES */
void treexprint(struct tnode *p)
{
 if(p != NULL) {
  treexprint(p->left);
  if(p->match)
   printf("%s\n",p->word);
  treexprint(p->right);
 }
}


//getword: get next word or character from input
int getword(char *word, int lim)
{
 int c, d, comment(void), getch(void);
 void ungetch(int);
 char *w = word;
 while(isspace(c = getch()))
  ;
 if(c !=EOF)
  *w++ = c;
 if(isalpha(c) || c == '-' || c == '#'){
  for(; --lim > 0; w++)
   if(!isalnum(*w = getch()) && *w != '-') {
    ungetch(*w);
    break;
   }
 } 
 else if(c == '\'' || c == '\\'){
  for(; --lim > 0; w++)
   if(((*w = getch()) == '\\')
     *++w = getch();
   else if(*w == c) {
    w++;
    break;
   }else if(*w == EOF)
    break;
 }else if(c == '/')
  if((d = getch()) == '*')
   c = comment();
  else
   ungetch(d);
 *w = '\0';
 return c;
}

Read more c programs
C Basic
C Strings
K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link