gpt4 book ai didi

c - 删除二维数组中的重复字符串值不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:52 25 4
gpt4 key购买 nike

我知道这个问题之前已经被问过很多次了,但是我是 C 数组的初学者,并且希望避免使用指针(如果可能)并使其尽可能简单。我已将用户的输入用于 char 数组,并希望删除程序中的所有重复字符串值

#include<string.h>
#include<stdio.h>

int main(){
int N, i, j, k;
int flag=1;
scanf("%d", &N);
if(1<=N<=10^6){
char a[N][5];
for(i=0; i<N; i++){
scanf("%s", &a[i]);
}
for(i=0; i<N; i++){
for(j=i+1;j<N;){
if(strcmp(a[i],a[j])==0){
for(k=j+1; k<N; k++){
memcpy(a[k], a[k+1], 5);
}
N--;
}else{
j++;
}
}
}
for(i=0; i<N; i++){
printf("%s\n", a[i]);
}
}
return 0;
}

enter image description here

输入 3 和 {"abc", "abc", "as"} 仅返回值 {"abc"}。我想将数组作为 {"abc", "as"}。我无法理解代码或逻辑中哪里出错了。

更新

我更改了下面提到的代码,但是对于更大的示例,它是连接字符串

#include<string.h>
#include<stdio.h>

int main(){
int N, i, j, k;

scanf("%d", &N);
if(1 <= N && N <= 1000000){
char a[N][5];
for(i=0; i<N; i++){
scanf("%5s", a[i]);
}
for(i=0; i<N; i++){
for(j=i+1;j<N;){
if(strcmp(a[i],a[j])==0){
for(k=j+1; k<N; k++){
strcpy(a[k-1], a[k]);
}
N--;
}else{
j++;
}
}
}
printf("%d\n", N);
for(i=0; i<N; i++){
printf("%s\n", a[i]);
}
}
return 0;
}

enter image description here

最佳答案

以下是对评论中所有错误的描述:

#include<string.h>
#include<stdio.h>

int main(){
int N, i, j, k;

// check the return value!
if (scanf("%d", &N) != 1) return 1;

// if(1<=N<=10^6){
// this is completely wrong:
// 1. ^ doesn't mean "power" but bitwise exclusive or
// 2. 1<=N evaluates to 0 (false) or 1 (true), this is ALWAYS <= 10
// 3. so you have finally 1^6 = 7 -- ALWAYS true as a boolean
//
// what you want is:
if(1 <= N && N <= 1000000){

char a[N][5];
for(i=0; i<N; i++){
// scanf("%s", &a[i]);
// at least limit the number of characters read (one less
// than your buffer size because there's a 0 byte added)
// then, taking a *pointer* of an array is wrong, the array
// already decays as a pointer, so leave out the `&`
scanf("%4s", a[i]);
}
for(i=0; i<N; i++){
for(j=i+1;j<N;){
if(strcmp(a[i],a[j])==0){
for (k=j+1; k<N; k++){
// memcpy(a[k], a[k+1], 6);
// two errors here:
// 1. You only have 5 bytes per element, so copy only 5
// 2. this is *off by one* for the array index
// correct version:
memcpy(a[k-1], a[k], 5);
// (or use strcpy())
}
N--;
}else{
j++;
}
}
}
for(i=0; i<N; i++){
printf("%s\n", a[i]);
}
}
return 0;
}

一般来说,总是启用编译器警告。您的编译器会为您发现大部分错误,看看当您在启用 gcc 和警告的情况下编译原始代码时会发生什么:

$ gcc -std=c11 -Wall -Wextra -pedantic -o2darr 2darr.c
2darr.c: In function 'main':
2darr.c:8:12: warning: comparison of constant '10' with boolean expression is always true [-Wbool-compare]
if(1<=N<=10^6){
^~
2darr.c:8:9: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
if(1<=N<=10^6){
~^~~
2darr.c:8:12: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
if(1<=N<=10^6){
~~~~^~~~
2darr.c:11:21: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[5]' [-Wformat=]
scanf("%s", &a[i]);
^
2darr.c:11:21: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[5]' [-Wformat=]
2darr.c:6:13: warning: unused variable 'flag' [-Wunused-variable]
int flag=1;
^~~~

关于c - 删除二维数组中的重复字符串值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44966308/

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