The problem is to write a program that sorts words input by the user and stops r
ID: 664643 • Letter: T
Question
The problem is to write a program that sorts words input by the user and stops reading when the user enters an empty word.
What is wrong with my code?
#include
#include
#include
#include
#define TOTALWORDS 20
#define LENGTH 20
int read_line(char str[], int n);
int main(){
int i;
int length;
int x = 0;
char word[LENGTH + 1];
char *words[TOTALWORDS];
for(;;){
if(x == TOTALWORDS){
printf("No space ");
break;
}
printf("Enter word: ");
length = read_line(word, TOTALWORDS);
if(length == 0){
break;
}
*(words + x) = malloc(length);
if(*(words + x) == NULL){
printf("Failure... Exiting ");
exit(0);
}
strcpy(*(length + x), word);
++x;
}
printf("In sorted order:");
for(i = 0; i < x; ++i){
printf("%s ", (*(length + i)));
free(*(length + i));
}
return 0;
}
int read_line(char str[], int n){
int ch, i = 0;
while ((ch = getchar()) != ' ') {
if (i < n){
str[i++] = ch;
}
}
str[i] = '';
return i;
}
I am getting an error at these line:
strcpy(*(length + x), word);
++x;
}
printf("In sorted order:");
for(i = 0; i < x; ++i){
printf("%s ", (*(length + i)));
free(*(length + i));
}
The error "invalid type argument of unary ‘*’ (have ‘int’)" at each line where there is a pointer. Pleae help!
#include
#include
#include
#include
#define TOTALWORDS 20
#define LENGTH 20
int read_line(char str[], int n);
int main(){
int i;
int length;
int x = 0;
char word[LENGTH + 1];
char *words[TOTALWORDS];
for(;;){
if(x == TOTALWORDS){
printf("No space ");
break;
}
printf("Enter word: ");
length = read_line(word, TOTALWORDS);
if(length == 0){
break;
}
*(words + x) = malloc(length);
if(*(words + x) == NULL){
printf("Failure... Exiting ");
exit(0);
}
strcpy(*(length + x), word);
++x;
}
printf("In sorted order:");
for(i = 0; i < x; ++i){
printf("%s ", (*(length + i)));
free(*(length + i));
}
return 0;
}
int read_line(char str[], int n){
int ch, i = 0;
while ((ch = getchar()) != ' ') {
if (i < n){
str[i++] = ch;
}
}
str[i] = '';
return i;
}
Explanation / Answer
This code have lot of compilation error
error: 'printf' was not declared in this scope printf("No space ");
error: 'printf' was not declared in this scope printf("Enter word: ");
error: 'malloc' was not declared in this scope *(words + x) = malloc(length);
error: 'NULL' was not declared in this scope if(*(words + x) == NULL)
error: 'exit' was not declared in this scope exit(0);
error: invalid type argument of unary '*' (have 'int') strcpy(*(length + x), word);
error: 'strcpy' was not declared in this scope strcpy(*(length + x), word);
error: 'printf' was not declared in this scope printf("In sorted order:");
error: invalid type argument of unary '*' (have 'int') printf("%s ", (*(length + i)));
error: invalid type argument of unary '*' (have 'int') free(*(length + i));
error: 'free' was not declared in this scope free(*(length + i)); In function 'int read_line(char*, int)':
error: 'getchar' was not declared in this scope
while ((ch = getchar()) != ' ') {
Below code will work
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define WORD_LEN 50
#define WORD_LIMIT 20
int read_line(char str[], int n);
int int_compare(const void* p, const void* q);
int main()
{
char n[WORD_LIMIT];
int i;
int *a;
a = malloc(n * sizeof(char));
for(i = 0; i < WORD_LEN; i++)
{
printf("Enter a word: ");
scanf("%c", &n[i]);
}
qsort(n, WORD_LEN, sizeof(char), int_compare);
printf("In sorted order: ");
for(i = 0; i < n; i++)
printf("%c ", a[i]);
printf(" ");
return 0;
}
int int_compare(const void* p, const void* q){
int n1 = *(int *) p;
int n2 = *(int *) q;
if (n1 < n2)
return -1;
if (n1 == n2)
return 0;
return 1;
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.