简体   繁体   中英

Reversing String in c using loops…

I have created a code reverse a string but for some reason it is not working. But I think my logic is right. Then why is it not working??

#include <stdio.h>
#include <stdlib.h>

int main() {
    char words[100];
    int i=0;
    printf("Enter a word/sentence: ");
    scanf("%s", words);

    while (words[i]!='\0') {
           ++i;
    }
    printf("\nThe Reverse is: ");
    while (i<=0) {
         printf("%s",words[i]);
         i--;
     }
     return 0;
}

While you already have an answer, there are a few additional points you need to consider before you have a solution that doesn't have the potential to invoke Undefined behavior .

First, always, always validate all user input. For all you know a cat could have gone to sleep on the 'L' key (with millions being entered), or a more likely case, the user just decides to type a 100-char sentence (or more) which leaves 'words' as an array of chars that is NOT nul-terminated and thus not a valid string in C. Your loop to get the length now invokes Undefined Behavior by reading beyond the end of words off into the stack until the first random '0' is encounter or a SegFault occurs.

To prevent this behavior (you should really just use fgets ) but with scanf you can provide a field-width modifier to prevent reading more than length - 1 chars. This insures space for the nul-terminating character.

Further, the "%s" conversion-specifier stops conversion on the first whitespace character encountered -- making your "Enter a .../sentence" an impossibility because scanf ("%s", words) will stop reading after the first word (at the first whitespace .

To correct this problem (you should really just use fgets ) or with scanf you can use a character class (stuff between [...] ) as the conversion specifier that will read until a '\\n' is encountered., eg scanf ("%[^\\n]", words) . However, recall, that is still not good enough because more than 99-chars can be entered leaving the string un-terminated at 100 and invoking Undefined Behavior at character 101 (off the end of the array).

To prevent this problem (ditto on fgets ), or include the field-width modifier, eg scanf ("%99[^\\n]", words) . Now no more than 99-chars will be read regardless of the cat sleeping on the 'L' key.

Putting that altogether, you could do something like:

#include <stdio.h>

#define MAXC 100    /* if you need a constant, define one */

int main(void) {

    char words[MAXC] = "";
    int i = 0, rtn = 0;     /* rtn - capture the return of scanf  */

    printf ("Enter a word/sentence : ");
    if ((rtn = scanf ("%99[^\n]", words)) != 1) {   /* validate ! */
        if (rtn == EOF)     /* user cancel? [ctrl+d] or [ctrl+z]? */ 
            fprintf (stderr, "user input canceled.\n");
        else                    /* did an input failure occur ? */
            fprintf (stderr, "error: invalid input - input failure.\n");
        return 1;               /* either way, bail */
    }

    for (; words[i]; i++) {}    /* get the length */

    printf ("Reversed word/sentence: ");
    while (i--)
        putchar (words[i]);     /* no need for printf to output 1-char */
    putchar ('\n');

    return 0;
}

Example Use/Output

$ ./bin/strrevloops
Enter a word/sentence : My dog has fleas.
Reversed word/sentence: .saelf sah god yM

Look things over and let me know if you have any further questions.

There are few mistakes in your program.

  1. After you have reached the end of the string.You should do i-- as your array index of i will be pointing to '\\0' .

  2. Your while loop checks for <= but it should be >= .

  3. Use %c for printing chararcters. %s is used to print strings and not char.


#include <stdio.h>
#include <stdlib.h>

int main() {
    char words[100];
    int i=0;
    printf("Enter a word/sentence: ");
    scanf("%s", words);

    while (words[i]!='\0') {
        ++i;
    }
    i--;
    printf("\nThe Reverse is: ");
    while (i>=0) {
       printf("%c",words[i]);
       i--;
    }
 return 0;
}

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