Question

In: Computer Science

C Code Edit this code to make it output all unique letters IN LOWERCASE of an...

C Code

Edit this code to make it output all unique letters IN LOWERCASE of an input file.

For example, with this input:

Raspberry Grapefruit Straw berry raspBERRY

Blue$$berry apple Pine_apple raspberry

The output should be:

raspberry

grapefruit

straw

berry

blue

berry

apple

pine

NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output.

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

int main()
{
    //Pointer to access the file.
   FILE *file_ptr;

   //string to read each word.
   char word[100];


   // To open the file.
   file_ptr = fopen("input_text.txt", "r");
   if (file_ptr == NULL)
   {
       printf("No such file exist in current folder \n");
       exit(0);
   }


   //To read the file .
   while ( fscanf(file_ptr, "%s", word) != EOF)
   {
        printf("Word (%ld): ",strlen(word));
       printf ("%s\n", word);
   }
  
   //Close the file.
   fclose(file_ptr);
   return 0;
}

Solutions

Expert Solution

Please let me know if anything is required.

NOTE: The above output contain berry twice which is wrong.

Copyable code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
//Pointer to access the file.
FILE *file_ptr;

//string to read each word.
char word[100];
char uniq_word[100][100];
int f;

// To open the file.
file_ptr = fopen("input_text.txt", "r");
if (file_ptr == NULL)
{
printf("No such file exist in current folder \n");
exit(0);
}



//To read the file .
int w=0;
while ( fscanf(file_ptr, "%s", word) != EOF)
{
char temp[strlen(word)];//to store the temperory word
temp[0] = '\0';
for(int i=0;i<strlen(word);i++)
{
char c=tolower(word[i]);//converting each character to lower case to ignore case
if(c>='a' && c<='z') //condition for only character not any other characters like $,_
{
strncat(temp,&c,1);//adding the character to the temperory string
  
}
else
{
f=0; //if any special character ignoring it and starting as a new string
//condition for the word is not repeated
if(strlen(temp)>0) // if the word length is greater than 0
{
for(int k=0;k<w;k++)
{
  
if(strcmp(uniq_word[k],temp)==0) ///if the word repeated
{
f=1;
}
  
}
  
if(f==0)
{
strcpy(uniq_word[w++],temp);//if the word not repeated
}
}
  
temp[0] = '\0';
  


}

}
f=0;
//condition for the word is not repeated
if(strlen(temp)>0)
{
for(int k=0;k<w;k++)
{
  
if(strcmp(uniq_word[k],temp)==0)//if the word repeated
{
f=1;
}
  
}
  
if(f==0)
{
strcpy(uniq_word[w++],temp);//if the word is not repeated
}
  
}

}
//printing the results
for(int i=0;i<w;i++)
{
printf("%s\n",uniq_word[i]);
}
  
//Close the file.
fclose(file_ptr);
return 0;
}

Sample output:

new code as per comment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
//Pointer to access the file.
FILE *file_ptr;

//string to read each word.
char word[100];
char uniq_word[100][100];
int f;

// To open the file.
file_ptr = fopen("input_text.txt", "r");
if (file_ptr == NULL)
{
printf("No such file exist in current folder \n");
exit(0);
}



//To read the file .
int w=0;
while ( fscanf(file_ptr, "%s", word) != EOF)
{
char temp[strlen(word)];//to store the temperory word
temp[0] = '\0';
for(int i=0;i<strlen(word);i++)
{
char c=tolower(word[i]);//converting each character to lower case to ignore case
if(c>='a' && c<='z') //condition for only character not any other characters like $,_
{
strncat(temp,&c,1);//adding the character to the temperory string
  
}
else
{
f=0; //if any special character ignoring it and starting as a new string
//condition for the word is not repeated
// if(strlen(temp)>0) // if the word length is greater than 0
{
for(int k=0;k<w;k++)
{
  
if(strcmp(uniq_word[k],temp)==0) ///if the word repeated
{
f=1;
}
  
}
  
if(f==0)
{
strcpy(uniq_word[w++],temp);//if the word not repeated
}
}
  
temp[0] = '\0';
  


}

}
f=0;
//condition for the word is not repeated
// if(strlen(temp)>0)
{
for(int k=0;k<w;k++)
{
  
if(strcmp(uniq_word[k],temp)==0)//if the word repeated
{
f=1;
}
  
}
  
if(f==0)
{
strcpy(uniq_word[w++],temp);//if the word is not repeated
}
  
}

}
//printing the results
for(int i=0;i<w;i++)
{
printf("%s\n",uniq_word[i]);
}
  
//Close the file.
fclose(file_ptr);
return 0;
}

Sample output:


Related Solutions

How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to convert a...
The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to convert a lowercase letter to uppercase, subtract 32 from it. Use this information to write a program that reads characters from the keyboard. Have it convert all lowercase letters to uppercase, and all uppercase letters to lowercase, displaying the result. Make no changes to any other character. Have the program stop when the user enters a period. At the end, have the program display the number...
A seqstring is a sequence of 18 letters(one of the 26 lowercase letters a-z). How many...
A seqstring is a sequence of 18 letters(one of the 26 lowercase letters a-z). How many seq strings are there where the number of letters between any two occurences of the same letter is at least 2?
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Edit the given program to produce the following output in c++ mode: - Take in the...
Edit the given program to produce the following output in c++ mode: - Take in the name of a superhero & tell him how many villains he/she has to defeat today - The number of villains is randomly generated and should be a number between 11 and 42. - Use a seed of 7. Hint: Compile the program first before making edits What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The method signature is as follows:  public int count(String s)
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT