1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char slovo(char *x);
int main ()
{
    char x;
    x=(char *)malloc(50*sizeof(char));
    fgets(x,sizeof(x),stdin);
    printf("%s",slovo(x));
    return 0;
    
}

char slovo(char *x)
{
    int i,n;
    n=strlen(x);
    for(i=0;i<n;i++)
    {
        if ((x[i]>='A' && x[i]<='Z') || (x[i]>='a' && x[i]<='z')) x[i]=x[i];
        else x[i]= ('A'+(rand()%26) || ('a'+(rand()%26)));
    }
    return x;
    
}

The task is to enter a string with a max of 50 characters and also allocate the memory for 50 characters. If a character in the string isnt a letter I must randomly convert it to any letter. I have a lot of errors and I dont know what to do.

8 3 C:\Users\x\Documents\juarsr.c [Warning] assignment makes integer from pointer without a cast

9 8 C:\Users\x\Documents\juarsr.c [Warning] passing argument 1 of 'fgets' makes pointer from integer without a cast

10 20 C:\Users\x\Documents\juarsr.c [Warning] passing argument 1 of 'slovo' makes pointer from integer without a cast

4 6 C:\Users\x\Documents\juarsr.c [Note] expected 'char ' but argument is of type 'char'

24 2 C:\Users\x\Documents\juarsr.c [Warning] return makes integer from pointer without a cast

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Also, this didn't raise a warning, but in line 7 of `slovo`, you're actually setting `x[i]` to the logical `or` of those two values, which will in fact always be `1` since both of them will be non-zero. Instead, you want to do something like `else x[i]=(rand() % 2) ? ('A'+(rand()%26)):('a'+(rand()%26));`. – Atticus Stonestrom Aug 27 '20 at 17:12
  • 1
    you seem to don't understand `char` and `char*` and the difference between them. I suggest you go back and re-read your lectures and start with a good book [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – bolov Aug 27 '20 at 17:12
  • (If you haven't come across the ternary operator in `C` (ie the `__ ? __ : __` operator), that line of code basically returns the value on the right of the colon if the expression on the left of the question mark is 0, and returns the value on the left of the colon otherwise) – Atticus Stonestrom Aug 27 '20 at 17:23
  • @jurepure123 What is the great sense of this assignment x[i]=x[i]? – Vlad from Moscow Aug 27 '20 at 17:40

1 Answers1

0

In this code snippet

char x;
x=(char *)malloc(50*sizeof(char));

you declared the identifier x having the type char but you are trying to assign it with an object of the type char *.

You need to declare a pointer

char *x;

In this call

fgets(x,sizeof(x),stdin);

again you are using the object x of the type char. But if you will redeclare the identifier as it is required that is as having the type char * nevertheless this call will be incorrect because sizeof( x ) yields the size of pointer that is not equal to 50.

You have to write

fgets(x, 50 ,stdin);

Also you need to remove the new line character '\n' that can be appended to the entered string by the function fgets.

The function slovo should be declared like

char * slovo( char *x );

because within the function you are returning the pointer x.

The assignment x[i] to x[i] in this if statement

if ((x[i]>='A' && x[i]<='Z') || (x[i]>='a' && x[i]<='z')) x[i]=x[i];

does not make a sense.

In the else sub-statement

else x[i]= ('A'+(rand()%26) || ('a'+(rand()%26)));

the object x[i] will always have the value 1 because it is the value of the logical OR expression using in the right hand side of the assignment.

The program can look the following way.

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

char * slovo( char *s )
{
    const char *letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const size_t total = 26;
    enum { LowerCase = 0, UpperCase = 1 };
    
    for ( char *p = s; *p; ++p )
    {
        if ( strchr( letter, toupper( ( unsigned char )*p ) ) == NULL )
        {
            char c = letter[ rand() % total] ;
            if ( rand() % 2 == LowerCase ) c = tolower( c );
            *p = c;
        }
    }
    
    return s;
}

int main(void) 
{
    const size_t N = 50;
    char *s = malloc( N );
    
    fgets( s, N, stdin );
    
    s[strcspn( s, "\n" )] = '\0';
    
    puts( slovo( s ) );

    free( s );
    
    return 0;
}

If to enter the string "Hello World!" then its output can look like

HellonWorldL
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 28 25 C:\Users\x\Documents\asfa.cpp [Error] invalid conversion from 'void*' to 'char*' [-fpermissive] – jurepure123 Aug 28 '20 at 00:06
  • @jurepure123 It means that you are compiling your program as a C++ program instead of a C program. In C++ you have to cast the return pointer from nalloc. That is you need to write char *s = ( char * ) malloc( N ); – Vlad from Moscow Aug 28 '20 at 07:56