Fast
New Coder
Hi, i'm a newbie and my teacher gave me an exercis with strings and arrays where i had to lowercase, uppercase ecc a string.
Now i'm stuck with my code where i have to replace a random letter of the string with a random letter without using any library that simplify the problem.
That's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
time_t t;
char str1[57];
int opzione = 0;
int c;
char i = "abcdefghijklmnopqrstuvxyz" [rand() % 26];
while ((c = getchar()) != '\n' && c != EOF);
printf("Inserisci una stringa\n");
fgets(str1, 57, stdin);
printf("Premi da 1 a 5 per scegliere l'operazione desiderata.\n");
printf("1. Rendi la stringa tutta minuscola. 2. Rendi la stringa tutta maiuscola. 3. Elimina dalla stringa tutti i caratteri diversi dalle lettere dell'alfabeto. 4. Scambiare tutte le occorenze di una lettera a caso con quelle di un'altra lettera a caso. 5. Cifra la stringa.\n");
scanf("%d", &opzione);
switch (opzione) {
case 1:
for (c = 0; c <= strlen (str1); c++) {
if (str1[c] >= 65 && str1[c] < 97) {
str1[c] = str1[c] + 32;
}
}
printf("%s",str1);
break;
case 2:
for (c = 0; c <= strlen (str1); c++) {
if (str1[c] >= 97 && str1[c] <= 122) {
str1[c] = str1[c] - 32;
}
}
printf("%s",str1);
break;
case 3:
while (c < sizeof(str1)) {
if ((str1[c] != '\0' && str1[c] < 'A') || str1[c] > 'z')
{
str1[c] = ' ';
}
{
c++;
}
}
printf("%s\n", str1);
break;
case 4:
srand((unsigned) time(&t));
for (i = 0; i <= strlen (str1); i++)
{
printf("%c", str1);
}
break;
case 5:
break;
}
}
Now i'm stuck with my code where i have to replace a random letter of the string with a random letter without using any library that simplify the problem.
That's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
time_t t;
char str1[57];
int opzione = 0;
int c;
char i = "abcdefghijklmnopqrstuvxyz" [rand() % 26];
while ((c = getchar()) != '\n' && c != EOF);
printf("Inserisci una stringa\n");
fgets(str1, 57, stdin);
printf("Premi da 1 a 5 per scegliere l'operazione desiderata.\n");
printf("1. Rendi la stringa tutta minuscola. 2. Rendi la stringa tutta maiuscola. 3. Elimina dalla stringa tutti i caratteri diversi dalle lettere dell'alfabeto. 4. Scambiare tutte le occorenze di una lettera a caso con quelle di un'altra lettera a caso. 5. Cifra la stringa.\n");
scanf("%d", &opzione);
switch (opzione) {
case 1:
for (c = 0; c <= strlen (str1); c++) {
if (str1[c] >= 65 && str1[c] < 97) {
str1[c] = str1[c] + 32;
}
}
printf("%s",str1);
break;
case 2:
for (c = 0; c <= strlen (str1); c++) {
if (str1[c] >= 97 && str1[c] <= 122) {
str1[c] = str1[c] - 32;
}
}
printf("%s",str1);
break;
case 3:
while (c < sizeof(str1)) {
if ((str1[c] != '\0' && str1[c] < 'A') || str1[c] > 'z')
{
str1[c] = ' ';
}
{
c++;
}
}
printf("%s\n", str1);
break;
case 4:
srand((unsigned) time(&t));
for (i = 0; i <= strlen (str1); i++)
{
printf("%c", str1);
}
break;
case 5:
break;
}
}