Ive written this code to try and read if there are any duplicate letters in a word, but I keep coming across this error:
Error: value computed is not used [-Werror=unused-value]
The line in question is:
arr[(int)(str[i]) ];
The whole code is:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int arr[256]={0};
for(i=0;i<strlen(str);i )
{
if(str[i]==' '){
continue;
arr[(int)(str[i]) ];
}
}
printf("Repeated character in a string are:\n");
for(i=0;i<256;i )
{
if(arr[i]>1)
{
printf("%c occurs %d times\n",(char)(i),arr[i]);
}}
return 0;
}
Here is the error message from the console: https://i.stack.imgur.com/GpMOW.png
Any help is appreciated :)
CodePudding user response:
I've made some changes to the code, shown in comment.
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf(")[^\n]", str); // limit the input length
int i;
int arr[256] = { 0 };
for(i = 0; i < strlen(str); i )
{
if(str[i] == ' '){
continue;
}
// this line was repositioned so that it can execute
// char can be signed, so don't index by a negative value
// the post-increment should apply to the `arr[]` array element
arr[ (unsigned char)str[i] ] ;
}
printf("Repeated character in a string are:\n");
for(i = 0; i < 256; i )
{
if(arr[i] > 0) // inform *any* usage
{
// (char)i gets promoted to int anyway
printf("%c occurs %d times\n", i, arr[i]);
}
}
return 0;
}
Session:
Enter your String:one two
Repeated character in a string are:
e occurs 1 times
n occurs 1 times
o occurs 2 times
t occurs 1 times
w occurs 1 times