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

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 program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT