[英]Reverse an array in C Programming
我被困在我一直在處理的一段代碼上。 我需要使用 argc 和 argv[] 參數接收 N 個輸入。 然后 N 個輸入將允許用戶輸入那么多的句子。 對於每個句子,我的代碼應該反轉句子中的每個單詞。 目前,我的代碼將接收 N 值和句子,但不會打印反向句子。 相反,它打印一個空行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 80
void get_input(char *line){
fgets(line, SIZE, stdin);
char *ptr = strchr(line, '\n');
if (ptr){
*ptr = '0'; }
}
void reverse(char *line){
char copy[SIZE];
char word[SIZE];
memset(copy, 0, SIZE);
int line_len = strlen(line);
int word_len = 0;
int i;
for(i=line_len; i<=0; --i){
if(line[i] == ' ' && word_len > 0){
memset(word, 0, SIZE);
strncpy(word, line + i + 1, word_len);
strcat(copy, word);
strcat(copy, " ");
word_len = 0;
}else if(isalnum(line[i]) || line[i] == '\'')
{word_len++;}
}
if(word_len>0){
memset(word, 0, SIZE);
strncpy(word, line, word_len);
strcat(copy, word);}
strcpy(line, copy);
}
int main(int argc, char *argv[]){
int N = (int)strtol(argv[1], NULL, 10);
if(N<0){
printf("ERROR: Please provide an integer greater than or equal to 0\n");
return 0;
}
if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n");
return 0;
}
char line[SIZE];
int i;
for(i=0;i<N;i++){
get_input(line);
reverse(line);
printf("%s\n", line);
}
return 0;
}
示例輸入:
1
狐狸跳過了一根原木
示例所需的輸出:
記錄一只跳過的狐狸
電流輸出:
你把<=
而不是>=
for(i = line_len; i <= 0; --i) {
一個簡單的愚蠢錯誤:
for(i=line_len; i<=0; --i){
這個循環有什么作用? 好吧,首先它將i
設置為line_len
。 然后在i
小於或等於 0時循環,並在每個循環結束時將i
減一。
如果line_len
為零,這將循環很長時間(直到i
溢出),否則它根本不會循環(因為i
將大於 0)。
您的意思可能是>=
而不是<=
。
您也可能指的是i=line_len-1
而不是i=line_len
,但這很難說。
編輯:將 for 循環中的 i<=0 更改為 i>=0。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 80
void get_input(char *line){
fgets(line, SIZE, stdin);
char *ptr = strchr(line, '\n');
if (ptr){
*ptr = ' '; }
}
void reverse(char *line){
char copy[SIZE];
char word[SIZE];
memset(copy, 0, SIZE);
int line_len = strlen(line);
int word_len = 0;
int i;
for(i=line_len; i>=0; --i){
if(line[i] == ' ' && word_len > 0){
memset(word, 0, SIZE);
strncpy(word, line + i + 1, word_len);
strcat(copy, word);
strcat(copy, " ");
word_len = 0;
}else if(isalnum(line[i]) || line[i] == '\'')
{word_len++;}
}
if(word_len>0){
memset(word, 0, SIZE);
strncpy(word, line, word_len);
strcat(copy, word);}
strcpy(line, copy);
}
int main(int argc, char *argv[]){
int N = (int)strtol(argv[1], NULL, 10);
if(N<0){
printf("ERROR: Please provide an integer greater than or equal to 0\n");
return 0;
}
if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n");
return 0;
}
char line[SIZE];
int i;
for(i=0;i<N;i++){
get_input(line);
reverse(line);
printf("%s\n", line);
}
return 0;
}
非常好的逆轉嘗試。 但是,您可能使它變得比需要的更困難。 如果您只是簡單地顛倒參數的順序,您可以將它們反向打印出來,或者如果您想從顛倒的參數中構建一個字符串,只需使用strcat
以相反的順序連接它們。 有很多方法可以做到這一點,但一個相當直接的方法是:
#include <stdio.h>
#include <string.h>
#define MAXC 1024
int main (int argc, char **argv) {
char string[MAXC] = "";
int i, len = 0;
for (i = argc - 1; i && len + strlen (argv[i]) + 2 < MAXC; i--) {
strcat (string, argv[i]);
strcat (string, " ");
len = strlen (string);
}
printf ("%s\n", string);
return 0;
}
它只是從最后一個參數索引argc - 1
,然后只要參數索引大於0
( i
) 並且您在允許的字符數len + strlen (argv[i]) + 2 < MAXC
( +2
允許空終止符和' '
),將參數連接到字符串strcat (string, argv[i]);
並添加一個空格strcat (string, " ");
(您可以在strcat (string, " ");
之前添加if (i > 1)
strcat (string, " ");
以消除尾隨空格)。
示例使用/輸出
$ ./bin/str_revargs The fox jumped over a log
log a over jumped fox The
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.