gpt4 book ai didi

c - C. 凯撒 block 移位中数组输入按一个索引关闭

转载 作者:行者123 更新时间:2023-11-30 16:27:00 28 4
gpt4 key购买 nike

这里我有从标准输入将字符流输入到数组中的代码。然后将该数组转换为二维数组。然后,它将该数组从行列顺序更改为列行顺序。然后它打印出创建凯撒移位加密的新数组。我遇到的问题是我的数组开始使用第二个用户输入的字符将字符输入到数组中,我不确定为什么。

例如,如果我在控制台中输入“Hello”,则只有“ello”被放入数组中。

有什么想法吗?谢谢!

//
// main.c
// Caesar Block Cypher
//
// Created by Jacob Byerline on 10/20/18.
// Copyright © 2018 Jacob Byerline. All rights reserved.
//
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main(void){
//The size is extended by the input with the value of the provisional

char *str;
int inputChar;
int i, j;
size_t size = 0;
size_t len = 0;

printf("input string : ");
inputChar = getchar();

str = realloc(NULL, sizeof(char)*size);//size is start size
if(!str)return *str;

while(EOF!=(inputChar=fgetc(stdin)) && inputChar != '\n'){

if(isalpha(inputChar)){

str[len++]=inputChar;

if(len==size){

str = realloc(str, sizeof(char)*(size+=16));
if(!str)return *str;
}
}

}
str[len++]='\0';

int squareOfLen = sqrt(len);
int twoDimensionalArraySize = squareOfLen + 1;

//printf("%lu \n", len);
//printf("%d \n", squareOfLen);
//printf("%d \n", twoDimensionalArraySize);

char twoDstr[twoDimensionalArraySize][twoDimensionalArraySize];
char FINALtwoDstr[twoDimensionalArraySize][twoDimensionalArraySize];

memcpy(twoDstr, str, twoDimensionalArraySize * 2 * sizeof(int));

for(i=0; i<twoDimensionalArraySize; i++){
for(j=0; j<twoDimensionalArraySize; j++){
printf("2D Array[%d][%d] = %c\n",i,j,twoDstr[i][j]);
}
}

for(i=0; i<twoDimensionalArraySize; i++){
for(j=0; j<twoDimensionalArraySize; j++){
FINALtwoDstr[i][j] = twoDstr[j][i];
//printf("2D Array[%d][%d] = %c\n",i,j,FINALtwoDstr[i][j]);
printf("%c",FINALtwoDstr[i][j]);
}
}

printf("\n");
return 0;

}

最佳答案

删除

inputChar = getchar();

这将从您的输入流中占用“H”。

参见此处https://ideone.com/ZwT9Iq

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main(void){
//The size is extended by the input with the value of the provisional

char *str;
int inputChar;
int i, j;
size_t size = 0;
size_t len = 0;

printf("input string : ");
//inputChar = getchar();

str = realloc(NULL, sizeof(char)*size);//size is start size
if(!str)return *str;

while(EOF!=(inputChar=fgetc(stdin)) && inputChar != '\n'){

if(isalpha(inputChar)){

str[len++]=inputChar;

if(len==size){

str = realloc(str, sizeof(char)*(size+=16));
if(!str)return *str;
}
printf("%c", inputChar);

}

}
printf("\n");
str[len]='\0';

int squareOfLen = sqrt(len);
int twoDimensionalArraySize = squareOfLen + 1;

//printf("%lu \n", len);
//printf("%d \n", squareOfLen);
//printf("%d \n", twoDimensionalArraySize);

char twoDstr[twoDimensionalArraySize][twoDimensionalArraySize];
char FINALtwoDstr[twoDimensionalArraySize][twoDimensionalArraySize];

memcpy(twoDstr, str, twoDimensionalArraySize * 2 * sizeof(int));

for(i=0; i<twoDimensionalArraySize; i++){
for(j=0; j<twoDimensionalArraySize; j++){
printf("2D Array[%d][%d] = %c\n",i,j,twoDstr[i][j]);
}
}

for(i=0; i<twoDimensionalArraySize; i++){
for(j=0; j<twoDimensionalArraySize; j++){
FINALtwoDstr[i][j] = twoDstr[j][i];
//printf("2D Array[%d][%d] = %c\n",i,j,FINALtwoDstr[i][j]);
printf("%c",FINALtwoDstr[i][j]);
}
}

printf("\n");
return 0;

}

输出

Hleol

关于c - C. 凯撒 block 移位中数组输入按一个索引关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913770/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com