In: Computer Science
```please convert this code to make only using for loop not while loop
#include
#include
#define MAX_SIZE 500
int main()
{
char str[MAX_SIZE];
char tosearch[MAX_SIZE];
char part1[100];
char part2[100];
int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0;
int j = 0;
int a = 0;
int b = 0;
int total = 0;
printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(tosearch);
for (i = 0; i < strlen(tosearch); i++)
{
if (tosearch[i] == '*')
{
for (j = 0; j < i; j++)
{
part1[j] = tosearch[j];
}
a = j;
}
}
for (j = 0; j < strlen(tosearch) - a; j++)
{
part2[j] = tosearch[a + j + 1];
}
for (j = 0; j < a; j++)
{
printf("%c", part1[j]);
}
printf("\n");
for (j = 0; j < strlen(tosearch) - a - 1; j++)
{
printf("%c", part2[j]);
}
printf("\n");
b = strlen(part2);
while (cursor < strlen(str)) { // while the cursor is smaller
than length of main string while loop goes on
while (cnt1 != a) { // if the cnt1 is smaller than the part1
length the while loop goes on
if (str[cursor] == part1[0]) { // if the str[cursor] is correspond
to the first word of part1 go to for loop
for (i = 0; i < a; i++) {
if (str[cursor + i] == part1[i])
{
cnt1++; // count the number of matching letter of part1 and main
string
}
else { // else the cnt1 goes to 0 again and cursor goes
forward
cnt1 = 0;
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt1 == a)
{
break;
}
cursor++; // cursor goes to the next letter
}
cursor += a; printf("the value of cursor %d ", cursor);
while (cnt2 != b) { // if cnt2 is less than the length of part2 the while loop goes on
if (str[cursor] == part2[0]) { // if the str[cursor] correspond
with part2[0] for loop goes on
for (i = 0; i < b; i++) {
if (str[cursor + i] == part2[i])
{
cnt2++; // count the matching letter of str and part2
}
else {
cnt2 = 0; // else cnt2 goes to 0 and cursor move on break free from
the for loop
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt2 == b) // if the cursor is
larger than the strlen or cnt2 == (the length of part2) break
free
break;
cursor++; // move on to the next letter of the main string
}
if (cursor + b > strlen(str))
break;
cnt++;
cnt1 = 0;
cnt2 = 0;
}
printf("tot %d\n", cnt);
}
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 500
int main()
{
char str[MAX_SIZE];
char tosearch[MAX_SIZE];
char part1[100];
char part2[100];
int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0;
int j = 0;
int a = 0;
int b = 0;
int total = 0;
printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(tosearch);
for (i = 0; i < strlen(tosearch); i++)
{
if (tosearch[i] == '*')
{
for (j = 0; j < i; j++)
{
part1[j] = tosearch[j];
}
a = j;
}
}
for (j = 0; j < strlen(tosearch) - a; j++)
{
part2[j] = tosearch[a + j + 1];
}
for (j = 0; j < a; j++)
{
printf("%c", part1[j]);
}
printf("\n");
for (j = 0; j < strlen(tosearch) - a - 1; j++)
{
printf("%c", part2[j]);
}
printf("\n");
b = strlen(part2);
for (;cursor < strlen(str);) { // while the cursor is smaller than length of main string while loop goes on
for(;cnt1 != a;) { // if the cnt1 is smaller than the part1 length the while loop goes on
if (str[cursor] == part1[0]) { // if the str[cursor] is correspond to the first word of part1 go to for loop
for (i = 0; i < a; i++) {
if (str[cursor + i] == part1[i])
{
cnt1++; // count the number of matching letter of part1 and main string
}
else { // else the cnt1 goes to 0 again and cursor goes forward
cnt1 = 0;
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt1 == a)
{
break;
}
cursor++; // cursor goes to the next letter
}
cursor += a; printf("the value of cursor %d ", cursor);
for (;cnt2 != b;) { // if cnt2 is less than the length of part2 the while loop goes on
if (str[cursor] == part2[0]) { // if the str[cursor] correspond with part2[0] for loop goes on
for (i = 0; i < b; i++) {
if (str[cursor + i] == part2[i])
{
cnt2++; // count the matching letter of str and part2
}
else {
cnt2 = 0; // else cnt2 goes to 0 and cursor move on break free from the for loop
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt2 == b) // if the cursor is larger than the strlen or cnt2 == (the length of part2) break free
break;
cursor++; // move on to the next letter of the main string
}
if (cursor + b > strlen(str))
break;
cnt++;
cnt1 = 0;
cnt2 = 0;
}
printf("tot %d\n", cnt);
}
Converted all while loops into for loops