如何将下面的for循环转换为while循环?如何将循环转换为while循环?
for (j = 0; s1[j] != '\0'; j++, i++)
sen[i] = s1[j];
我试图转换,但有任何空的结果,不知道什么是错的:
while (j= 0, s1[j] != '\0') {
j++, i++;
sen[i] = s1[j];
}
原来的代码如下:
#include <stdio.h>
#include <stdafx.h>
int main()
{
char s[] = "Bad";
char s1[] = "weather!";
char sen[10];
int i, j;
for (i = 0; i<20; ++i){
if (s[i] != '\0')
sen[i] = s[i];
else {
sen[i++] = ' ';
for (j = 0; s1[j] != '\0'; j++, i++)
sen[i] = s1[j];
break;
}
}
sen[i] = '\0';
puts(sen);
}
花时间阅读如何使用while循环。这可能对你有所帮助:https://en.wikipedia.org/wiki/While_loop – Cherubim
而不是编写'while -'-loops,或许你应该阅读['strcpy'](http://en.cppreference.com/ w/c/string/byte/strcpy)和['strcat'](http://en.cppreference.com/w/c/string/byte/strcat)。 –