Question

In: Computer Science

Write a C program that counts the number of repeated characters in a phrase entered by...

Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them.

If none of the characters are repeated, then print “No character is repeated”

For example:

If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10.

###Note: the output should print exactly as it is stated in the example if the user enters "full proof" ###

Solutions

Expert Solution

Code:

#include<stdio.h>
#include<string.h>
int main()
{
   char phrase[100],duplicate[100];
   int count=0,yes=0,i,j,k;
   printf("Enter a phrase:");
   gets(phrase);/*Reading user input*/
   for(i=0;phrase[i]!='\0';i++)
   {/*This loop iterates for every charecter*/
       if(phrase[i]==' ')
       {/*If space is present then we skip comparing*/
           continue;
       }
      
       for(j=i+1;phrase[j]!='\0';j++)
       {/*This loop iterates for phrase[i+1] to untill phrase[j]=="\0"*/
          
           if(phrase[i]==phrase[j])
           {/*IF phrase[i]==phrase[j]*/
               yes=0;
               for(k=0;k<count;k++)
               {
                   if(duplicate[k]==phrase[i])
                   {/*If the charecter alredy present in the duplicate array*/
                       yes=1;
                   }
               }
               if(!yes)
               {/*If charecter is not present in the array
               then we add it to the duplicate array*/
                   duplicate[count]=phrase[i];
                   count++;
                   /*Here we increase the count*/
               }
           }
       }
   }
   if(count==0)
   {/*if count is 0 then we print no duplicates*/
       printf("No duplicates");
   }
   else
   {/*Else we print the no of duplicates and duplicate charecters*/
       printf("Number of Charecters Repeated:%d ",count);
       printf("Charecters repeated: ");
       for(i=0;i<count;i++)
       {
           if(i!=count-1)
           {
               printf("%c,",duplicate[i]);
           }
           else
           {
               printf("%c",duplicate[i]);  
           }
          
       }
   }
}

Output:

Indentation:


Related Solutions

Write a C program that counts the number of non white-space characters in an inputtext file....
Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file. The command format is as follows: command -f inputfile outputfile or, command -s inputfile -f indicates writing to an output file; -s indicates displaying the output on...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Write a C++ function to sort the characters in words in a inputed phrase. DO not...
Write a C++ function to sort the characters in words in a inputed phrase. DO not sort the characters in the string. Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output. Make the program as simple as possible. Use introductory Object-Oriented C++ programming. If all the requirements are met, then this will be upvoted. Do use the C++ Standard Library headers, do not use the C Standard...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT