查找数组的大小和单词随机播放
Finding size of array and word shuffle
我试图在我的程序中使用 size_t 查找字符串的长度,然后使用该整数来确定我的程序循环执行字母随机播放的次数(+6 到一个字母,所以= 保存时 g)。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stddef.h>
int main(void)
{
FILE *file;
int i, fnlen, lnlen;
char firstname[15], lastname[15], *ptr;
fflush(stdin);
printf("Please enter the first name of the player:");
if(fgets(firstname, sizeof(firstname), stdin) != NULL)
{
size_t fnlen = strlen(firstname);
}
printf("Please enter the last name of the player:");
if(fgets(lastname, sizeof(lastname), stdin) != NULL)
{
size_t lnlen = strlen(lastname);
}
for(i = 0; i < lnlen; i++)
{
lastname[i] = (char)((lastname[i] - 'a' + 4) % 26 + 'a');
}
for(i = 0; i < fnlen; i++)
{
firstname[i] = (char)((firstname[i] - 'a' + 4) % 26 + 'a');
}
file = fopen("text.txt", "wt");
if(!file)
{
printf("File is not able to open.");
exit(1);
}
fprintf(file, "Firstname is : %s\n""Lastname is: %s\n", firstname, lastname)
当我打开保存它的文件时,名字被正确打乱,但当我输入 5 时有 8 个字符,姓氏被正确保存,但字母没有打乱,它们只是输出我的内容在 fgets
中输入
我在您的代码中看到的主要问题是您在 if 语句中声明了 size_t 长度变量。这样,您对它们所做的任何更改都不会反映在 if 语句之外。鉴于此,我很惊讶您的代码编译(除非您还在向我们展示的内容之上声明了另一个 size_t len 和 fnlen,在这种情况下,通过在 if 语句中重新声明它,您正在隐藏它。)我会重写代码像这样。我想你可能在上面声明了它们并将它们初始化为零。在这种情况下,您在 if 语句中声明了一个完全不同的变量(具有相同的名称),它没有效果,并且循环中使用的长度变量仍然具有零值。
printf("Please enter the first name of the player:");
size_t fnlen = 0; //I declare them once outside of the if
size_t len = 0;
if(fgets(firstname, sizeof(firstname), stdin) != NULL)
{
fnlen = strlen(firstname); //do not redeclare them inside just assign
}
printf("Please enter the last name of the player:");
if(fgets(lastname, sizeof(lastname), stdin) != NULL)
{
len = strlen(lastname);
}
ptr = lastname;
while( *ptr != '\n') ++ptr;
*ptr = '[=10=]';
for(i = 0; i < len; i++)
{
lastname[i] = (char)((lastname[i] - 'a' + 6) % 26 + 'a');
}
for(i = 0; i < fnlen; i++)
{
firstname[i] = (char)((firstname[i] - 'a' + 6) % 26 + 'a');
}
我试图在我的程序中使用 size_t 查找字符串的长度,然后使用该整数来确定我的程序循环执行字母随机播放的次数(+6 到一个字母,所以= 保存时 g)。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stddef.h>
int main(void)
{
FILE *file;
int i, fnlen, lnlen;
char firstname[15], lastname[15], *ptr;
fflush(stdin);
printf("Please enter the first name of the player:");
if(fgets(firstname, sizeof(firstname), stdin) != NULL)
{
size_t fnlen = strlen(firstname);
}
printf("Please enter the last name of the player:");
if(fgets(lastname, sizeof(lastname), stdin) != NULL)
{
size_t lnlen = strlen(lastname);
}
for(i = 0; i < lnlen; i++)
{
lastname[i] = (char)((lastname[i] - 'a' + 4) % 26 + 'a');
}
for(i = 0; i < fnlen; i++)
{
firstname[i] = (char)((firstname[i] - 'a' + 4) % 26 + 'a');
}
file = fopen("text.txt", "wt");
if(!file)
{
printf("File is not able to open.");
exit(1);
}
fprintf(file, "Firstname is : %s\n""Lastname is: %s\n", firstname, lastname)
当我打开保存它的文件时,名字被正确打乱,但当我输入 5 时有 8 个字符,姓氏被正确保存,但字母没有打乱,它们只是输出我的内容在 fgets
中输入我在您的代码中看到的主要问题是您在 if 语句中声明了 size_t 长度变量。这样,您对它们所做的任何更改都不会反映在 if 语句之外。鉴于此,我很惊讶您的代码编译(除非您还在向我们展示的内容之上声明了另一个 size_t len 和 fnlen,在这种情况下,通过在 if 语句中重新声明它,您正在隐藏它。)我会重写代码像这样。我想你可能在上面声明了它们并将它们初始化为零。在这种情况下,您在 if 语句中声明了一个完全不同的变量(具有相同的名称),它没有效果,并且循环中使用的长度变量仍然具有零值。
printf("Please enter the first name of the player:");
size_t fnlen = 0; //I declare them once outside of the if
size_t len = 0;
if(fgets(firstname, sizeof(firstname), stdin) != NULL)
{
fnlen = strlen(firstname); //do not redeclare them inside just assign
}
printf("Please enter the last name of the player:");
if(fgets(lastname, sizeof(lastname), stdin) != NULL)
{
len = strlen(lastname);
}
ptr = lastname;
while( *ptr != '\n') ++ptr;
*ptr = '[=10=]';
for(i = 0; i < len; i++)
{
lastname[i] = (char)((lastname[i] - 'a' + 6) % 26 + 'a');
}
for(i = 0; i < fnlen; i++)
{
firstname[i] = (char)((firstname[i] - 'a' + 6) % 26 + 'a');
}