Ömer Çiftci
Ömer Çiftci

Reputation: 41

compiler doesn't see the second for loop

I am trying to change random character in string randomly. Also program will decide how many character will change randomly. Compiler doesn't see the second for loop. I don't know why? Thank you again

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

int main() {
    char string[10];
    srand(time(NULL));
    int a;
    int count = 0;
    printf("Please enter string: ");
    scanf("%s", string);
    for (a = 0; string[a] != '\0'; a++) {
        count++;
    }
    printf("%d\n", count);
    for (int i = 0; i <= count; i + rand() % count) {
        string[i];
    }
    printf("String is: %s ", string);
}

Upvotes: 1

Views: 72

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

To choose a random char in a word and replace it with a random char you can try the following.

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

int main() {
    char string[10];
    srand(time(NULL));
    int a;
    int count = 0;
    printf("Please enter string: ");
    scanf("%s", string);
    for (a = 0; string[a] != '\0'; a++) {
        count++;
    }
    printf("%d\n", count);
    char randomletter = 'a' + (random() % 26);
    string[rand() % count] = randomletter;
    printf("String is: %s ", string);
}

Test

Please enter string: foobar
6
String is: fooiar 

Test 2

Please enter string: zoobar
6
String is: zolbar 

Upvotes: 1

Related Questions