Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

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;
}
}
 
So what's the problem ? Where exactly are you stuck ?
(Edit) And please post code in proper code tags !
 
Last edited by a moderator:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom