简体   繁体   中英

I have a problem in C about reversing a string (reversing rows and letters

#include <stdio.h>
#include <string.h>
#define W 5
#define N 10
void print_(const char[W][N],int);
void reverse(char[W][N],int);
int main ( ) {
char words[W][N] ;
char test[N];
char endword[N]="end";
int i=0;

while (scanf("%9s", test), strcmp(test,endword)) {
strcpy(words[i++], test);
printf("%s\n", test);
if(i==W)break;
}
print_((const char(*)[N])words,i);
reverse(words,i);
print_((const char(*)[N])words,i);

return 0;
}
void print_(const char x[W][N],int j){
    int i;
    for(i=0;i<j;i++){
        printf("%d %s\n",i+1,x[i]);
    }
}
void reverse(char x[W][N],int max){
    int i,j,len,p=max;
    char temp[N],temporal[W][N];
    for(i=0;i<max;i++){
        strcpy(temp,x[i]);
        len=strlen(x[i]);
        for(j=0;j<len;j++){
            x[i][j]=temp[len-1-j];
        }
        memcpy(temporal[max-1],x[i],N*sizeof(char));
    }
    print_((const char(*)[N])temporal,max);

}

It manages to reverse the letters of each word but when i try to reverse the rows it doesn't work Here i try putting the memmory itself but that doesn't seem to work. The only thing that prints for temporal is the last x[max-1] and it is for temporal [max-1].

In the condition of the while loop

while (scanf("%9s", test), strcmp(test,endword)) {

there is used an expression with the comma operator,

It seems you meam

while ( scanf("%9s", test) == 1 && strcmp(test,endword) != 0 ) {

The function reverse reverses each row of the original array

    strcpy(temp,x[i]);
    len=strlen(x[i]);
    for(j=0;j<len;j++){
        x[i][j]=temp[len-1-j];
    }

But it does not reverse rows of the array.

This statement

memcpy(temporal[max-1],x[i],N*sizeof(char));

only copies a current row of the original array in the row max - 1 of the array temporal .

The function can be written for example the following way

void reverse( char x[W][N], size_t max )
{
    for ( size_t i = 0; i < max; i++ )
    {
        for ( size_t j = 0, n = strlen( x[i] ); j < n / 2; j++ )
        {
            char c = x[i][j];
            x[i][j] = x[i][n-j-1];
            x[i][n-j-1] = c;
        }
    }

    for ( size_t i = 0; i < max / 2; i++ )
    {  
        char temp[N];
        strcpy( temp, x[i] );
        strcpy( x[i], x[max-i-1] );
        strcpy( x[max-i-1], temp );
    } 
}

Here is a demonstration program

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

#define W 5
#define N 10

void reverse( char x[W][N], size_t max )
{
    for (size_t i = 0; i < max; i++)
    {
        for (size_t j = 0, n = strlen( x[i] ); j < n / 2; j++)
        {
            char c = x[i][j];
            x[i][j] = x[i][n - j - 1];
            x[i][n - j - 1] = c;
        }
    }

    for (size_t i = 0; i < max / 2; i++)
    {
        char temp[N];
        strcpy( temp, x[i] );
        strcpy( x[i], x[max - i - 1] );
        strcpy( x[max - i - 1], temp );
    }
}

int main( void )
{
    char s[W][N] = { "Happy", "New", "Year" };
    size_t max = 3;

    for (size_t i = 0; i < max; i++)
    {
        printf( "%s ", s[i] );
    }

    putchar( '\n' );

    reverse( s, max );

    for (size_t i = 0; i < max; i++)
    {
        printf( "%s ", s[i] );
    }

    putchar( '\n' );
}

The program output is

Happy New Year
raeY weN yppaH

Though it would be better to split the function into two functions. The first one reverses a string. And the second one reverses rows of a two-dimensional character array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM