vendredi 26 février 2021

rearranging a char array with strncpy in C

I am trying to change the sorting of a the arr list which could consist of zero, one, two as the inputted and stored values for arr. The stringreplace function is meant to shift every single element by one so the new sorting would be one, two, zero. I am trying to replace the elements with one another by using the strncpy function but I think it is a bit faulty, how could i fix this?

strncpy function

char stringreplace( char a[], int b){
    for(int j = 0; j > b -1; j++){
        strncpy(a[j], a[j+1], sizeof(a));}
    for(int j = 0; j > b; j++){
        printf("%s",a[j]);}
}

main function

int main()
{
    char input[100];
    char arr[100]= {0};
    int number;
    printf("Input the number of strings: ");
    scanf("%d", &number);
    for(int i= 0; i < number; i++){
        printf("Input the number of strings: ");
        scanf("%s", input);
        arr[i] = input;
    }
    stringreplace(arr, number);
    return 0;
}    

Aucun commentaire:

Enregistrer un commentaire