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 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 (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...
Write a C++ program that continuously requests a donation to be entered. If the donation is...
Write a C++ program that continuously requests a donation to be entered. If the donation is less than 0 or greater than 10, your program should print an appropriate message informing the user that an invalid donation has been entered, else the donation should be added to a total. When a donation of -1 is entered, the program should exit the repetition loop and compute and display the average of the valid donations entered.
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT