I know this question has been asked many times before, however I am a complete beginner to C arrays and would like to avoid using pointers (if possible) and keep it as simple as possible. I have used the input from the user for a char array and would like to remove any duplicate string values in my program
#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;
}
An input of 3 and {"abc", "abc", "as"} returns the value only {"abc"}. I would like to get the array as {"abc", "as"}. I cannot understand where in the code or logic, I have gone wrong.
Update
I changed the code as mentioned below however for larger examples it is concatenating the strings
#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;
}