简体   繁体   中英

using strlen in loop reversing a string in C

I'm trying to write a code where I input a String and I get it reversed. Ie: "nokia" to "aikon".

I already have it in a way, but now I want to use the function strlen() to get the original String's length and create the new one based on that length.

Here is the code:

void main(void)
{
    char palavra[10];
    char palavra2[10];
    int i;
    int k;

    printf("Introduza uma string: \n");
    scanf("%[^\n]", palavra);

    for( i = 0, k = strlen(palavra) - 1 ; i <= k; i++, k--)
    {
        palavra2[k] = palavra[i];
    }
    printf("\nString invertida: %s", palavra2);
}

However all I get from my printf() is a "?". What am I doing wrong?

your loop indexes are running the opposite ways so they meet in the middle and the loop stops when i <= k : half of the string is not inverted. Just test i against strlen(palavra)

Plus you have to null-terminate your string:

int len=strlen(palavra);
for( i = 0, k = len - 1 ; i < len; i++, k--)
{
    palavra2[k] = palavra[i];
}
palavra2[len]='\0';

(and you'd better store the value of strlen(palavra) so you have to compute it only once)

Try this simple reverse function:

/* reverse:  reverse string s in place */
void reverse(char s[])
{
    int c, i, j;

    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

To integrate it into your code:

void main(void)
{
    char palavra[10];
    int c, i, j;

    /* read the string */
    printf("Introduza uma string: \n");
    scanf("%[^\n]", palavra);

    /* null-terminate the string */
    palavra[strlen(palavra)] = '\0';

    /* reverse the string */
    for (i = 0, j = strlen(palavra)-1; i < j; i++, j--) {
        c = palavra[i];
        palavra[i] = palavra[j];
        palavra[j] = c;
    }

    printf("\nString invertida: %s", palavra);
}

The above answer are quite good , Iam just enhancing code by optimized it using bit operation.

#include<stdio.h>
int main(void)

{
    char palavra[10];

    printf("Introduza uma string: \n");
    scanf("%[^\n]", palavra);

    char *start=palavra;
    int len = strlen(palavra);
    char *end = (palavra+len-1);

    while(start < end )
    {
            *start = *start ^ *end;
            *end = *end ^ *start;
            *start = *end ^ *start;
            start++;
            end--;

    }


printf("\nString invertida: %s\n", palavra);

return 0;
}

It is good to use Bitwise operator and reducing number of iteration by half , inplace swapping

You can use recursive function. I am using string class (specific to C++), you can change it to char*.

void reverse(string& str, int i, int len)
{
    if(i<len)
        reverse(str, i+1, len-1);
    swap(str[i], str[len]);
}

Caller - reverse(str, 0, str.length()-1)

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