Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,497 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,713 people online right now. Registration is fast and FREE... Join Now!




Program that list the Anagram pairs from File

 
Reply to this topicStart new topic

Program that list the Anagram pairs from File

skan_33
4 Mar, 2008 - 12:57 AM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2008
Posts: 9

Hi,
I have some problem for writing code for anagram program...
The requirement is --------------
I have to write a program in such a way that it has to open a file which contains anagram pairs,so my code is to identify the all the anagram pairs and print it on the console
EX---->File contain
ABCDEFG
show
whos
apple
whos
pleap
stare
ate
eat
ABCDFEG
tea

output should be----->
ABCDEFG -> ABCDFEG
show->whos
apple->pleap
ate->eat
stare->Cannot find anagram match
tea->Cannot find anagram match
----------------------------------------------------------------------------------------------------------
I wrote the code but its not giving proper output,Plz check it out its urgent

Below is my Code----->
---------------------------------------------------------------------------------------------------
CODE

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


int            i,j,k,l,m,n;
char           str1len,str2len;
char           array[128][128];
char           string[128];

void fun(char *, char *);

int main(int argc, char *argv[]) {
        if (argc > 2 || argc < 2) {
                fprintf(stderr, "Error:Wrong number of arguments\n");
                fprintf(stderr, "Usage is: mailmerge <inputfile> <csvfile>\n");
                exit(8);
        }

        FILE  *fp1 = fopen(argv[1], "r");

        if (fp1 < 0) {
                fprintf(stderr, "File Open Error %s\n", argv[1]);
                exit(8);
        }

        if (fp1 == NULL) {
                printf("Oops(--):->Input file empty\n");
                exit(8);
        }

        while(fgets(string,sizeof string,fp1) !=NULL) {
                strcpy(array[i],string);
                 //printf("%s\n",array[i]);
                i++;
        }
        for(k=0;k<i;k++) {
                str1len = strlen(array[k])-1; //ignoring null char
                for(l=k+1;l<i;l++) {
                        str2len = strlen(array[l])-1; //ignoring null char
                        if(str1len == str2len) {
                                fun(array[k],array[l]);
                        }
                }
                if(str1len != str2len)
                        printf("%s->Cannot find anagram match\n",array[k]);
        }
}


void fun(char *string1, char *string2)
{
        str1len = strlen(string1)-1;  //ignoring new line char
        str2len = strlen(string2)-1;  //ignoring new line char
        //printf("string 1 %s \n",string1);
        //printf("string length 1 = %d \n",str1len);
        //printf("string 2 %s \n",string2);
        //printf("string length 2 = %d \n",str2len);

        char ch1,ch2;
        char *ptr1, *ptr2;
        int count1 = 0,count2;
        int test=1;

        //Removing new line char from string1
        if( (ptr1 = strchr(string1, '\n')) != NULL)
        *ptr1 = '\0';

        //Removing new line char from string2
        if( (ptr2 = strchr(string2, '\n')) != NULL)
        *ptr2 = '\0';

        for(m=0;m<str1len;m++)
        {
                ch1 = string1[m];
                //printf("%c\n",ch1);                   //read character from a string1

                if(test == 1)
                {
                        count1++;
                        for(n=0;n<str2len;n++)
                        {
                                ch2 = string2[n];       //read character from a string2
                                 //printf("%c\n",ch2);                                                          
                                if(ch1 == ch2)
                                {
                                        //printf("yes\n");
                                        test = 1;
                                        break;
                                }
                                else
                                {
                                        //printf("sorry\n");
                                        test = 0;
                                }
                        }
                }
                else
                {
                        break;
                }
        }
        if(str1len == count1) {
                printf("%s->%s\n",string1,string2);
        }
        else {
                printf("%s->Cannot find anagram match\n",string1);
        }
}


This post has been edited by skan_33: 4 Mar, 2008 - 04:25 AM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Program That List The Anagram Pairs From File
4 Mar, 2008 - 01:07 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
code.gif
User is offlineProfile CardPM
+Quote Post

skan_33
RE: Program That List The Anagram Pairs From File
5 Mar, 2008 - 10:20 AM
Post #3

New D.I.C Head
*

Joined: 23 Feb, 2008
Posts: 9

Hi
anybody is here to help me out in this problem
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 08:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month