In: Computer Science
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;
}
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: