简体   繁体   中英

printing common characters in three strings

Q) Write ac program that takes as input three strings, each of maximum 30 characters, and prints common characters present in all three strings. Each string can have different number of characters.

The code i have written is:

#include<stdio.h>

int main()
{
    int c1=0 , c2=0, c3=0 ,i,x;  
    int count1[26]={0} , count2[26]={0} , count3[26]={0};  
    char st1[30],st2[30],st3[30];  
    printf("enter string1: ");  
    gets(st1);  
    printf("\n enter string2: ");  
    gets(st2);  
    printf("\n enter string3: ");
    gets(st3); 

    while(st1[c1]!='\0')  
    {
        count1[st1[c1]-'a']++;  
        c1++;
    } 

    while(st2[c2]!='\0')    
    {
        count2[st2[c2]-'a']++;   
        c2++;
    }

    while(st3[c3]!='\0')
    {
        count3[st3[c3]-'a']++;
        c3++;
    }

    for(i=0;i<26;i++)
    {
        if(count1[i]!=0 && count2[i]!=0 && count3[i]!=0)
        {
            if (count1[i]<count2[i] && count1[i]<count3[i])
            {
                for(x=0;x<count1;x++)
                {
                    printf("\n %c ",x+'a');
                }
            }
            else if(count2[i]< count1[i] && count2[i]< count3[i])
            {
                for(x=0;x<count2;x++)
                {
                    printf("%c",x+'a');
                }
            }
            else
            {
                for(x=0;x<count3;x++)
                {
                    printf("%c",x+'a');
                }
            }
        }
    }
}

But when i run the code in codeblocks,continuous randomcharacters are occuring with continous beep beep sound till i shut down the system. can anyone help me with this?

There are a few issues that contribute to the problem.
Here they are in the order I solved them:

(Compile-time)

  1. As M Oehm mentioned in the comments, x < count1 is invalid as you are trying to compare an integer with a pointer. This should instead be x < count1[i] and so on for the other blocks where there is a similar comparison.
  2. gets() is unsafe and it's recommended to use gets_s() or fgets() instead. Here's why.

(Logical)

  1. printf("%c",x+'a'); should instead be printf("%c", i +'a'); ie you should take the character value of i (not x) before adding a to it as i is the variable keeping track of which alphabet you are looking at in that loop.
  2. If you only want to print out the alphabets that are repeated (eg abc ), not the sequence of characters ( aabbccc ), the if , else if , else and for sections within your final for(i=0;i<26;i++) loop are unnecessary. All these can be removed safely and the final section of your code could be replaced with the following:

(please take note of the indentation and improve it throughout your program)

for(int i=0; i<26; i++)
{
    if(count1[i]!=0 && count2[i]!=0 && count3[i]!=0)
    {
        printf("%c", i+'a');
    }
}

There are various errors in your code.

  • When you print the common characters, your loop is:

     for (x = 0; x < count1; x++) ... 

    but count1 is the address of your array, which is usually a huge number. Your loop runs longer than you expect; it accesses memory locations beyond count[25] that contain random data (at least none your algorithm can make sense of) and prints it. (The beep sound comes from printing the character with ASCII code 7, the alert or bell code '\\a' . If you were on MS-DOS, you'd see some happy black and white faces too.)

    The correct loop runs to the number of letters stored in count1 for letter i .

     for (x = 0; x < count1[i]; x++) ... 
  • When you count letters, you increase

     count1[st1[c1] - 'a']++; 

    This will evoke the dreaded undefined behaviour when st1[c1] - 'a' is not in the range from 0 to 25. That can easily happen, eg if your string is "New York" . You either need to check that the character lies in the correct range or you need to extend your solution to the full range of characters. (Note that the assignment talks about characters, not letters.)

  • When you print the common characters, you don't print them correctly:

     printf("%c", x + 'a'); 

    Here, x is running from 0 to the number of occurrences of the letter i . If your common characters are thee o's, this will print "abc". You should print i + 'a' . (This is just a logic error, not something that can crash your program.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM