First of all I want to thank everyone who has helped me on my last issue, it helped me a lot with understanding of how a C program runs in the background and what loops are used for what. Now I'm encountered with this task: write a function which replaces every digit in a given string (0, 1, 2,... 9) (ASCII values: 48, 49, 50,... 57) with another random digit. Every other type of data in a string needs to remain untact. The task also says that, in main(), I'm supposed to create a string capable of holding 50 elements and do dynamic memory allocation for 50 char type elements - full capability of memory handling, and in the end print out the result of the function.
This is what I've written so far:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
char* switching(char s[]){
int i;
srand((unsigned)time(NULL));
for(i=0; i<strlen(s); i++){
if(s[i] > 57 || s[i] < 48){
s[i] = (float)rand()/RAND_MAX * 9;
}
}
return s;
}
int main(){
char *string = (char *)malloc(50);
if(string == NULL){
return 1;
}
char *r;
r = switching(string);
printf("%c" , r);
free(r);
return 0;
}
And this is what my apparent error is (although there might be more of them but I'm not sure) when i run it in OnlineGDB:
main.c:32:18: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
...Program finished with exit code 0
Press ENTER to exit console.
So yeah, as last time, any help would be appreciated. Thanks in advance to anyone that tries to help.
Aucun commentaire:
Enregistrer un commentaire