Jump to content

C Programming - Simple way to reverse a string

Right so I made this little C program that lets a user input a string and then outputs that input to the console.

 

I was wondering how I could reverse the string so that whatever the user enters is printed out backwards.

 

Would a for or while loop or something suffice? I'm not really sure how i'd do this to be honest. I'm quite new to C programming and just need something simple for now.

 

Here is the code for the program that just outputs the users input to the console.

#include <stdio.h>int main(){    char str[80];		    printf("Enter a string less than 80 characters: \n");	    scanf("%[^\n]", str);	    printf("Your string is: \"%s\"\n", str);	    return 0;}
   
   
Link to comment
Share on other sites

Link to post
Share on other sites

yes, I would use a for loop starting at the end and stepping back to the start, outputting those characters, or perhaps for more efficiency adding them to another string, and then dumping that to screen all at once

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

i'd probably implement a way to count the length of the string (without \n) and then loop trough it backwards makig a new string character per character, then print that.

Link to comment
Share on other sites

Link to post
Share on other sites

i'd probably implement a way to count the length of the string (without \n) and then loop trough it backwards makig a new string character per character, then print that.

Hmm, I do recall a function called Strleng, I'm assuming that counts the length of the string. I'll have to read up on it.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

I generally deal with C++ but I think this might work:

char newstring[80];int i;for (i = 79; i >= 0; i--)if (str[i] != 0)break;for (int j = 0; i >= 0; i--, j++)newstring[j] = str[i];

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites


#include <stdio.h>

int main()

{

int i; // iterator

int len; // cached strlen

char str[80];

char tmp; // For swapping

printf("Enter a string less than 80 characters: \n");

// Will read to str[79] OR '\n', whichever comes first

fgets(str, 80, stdin);

// Counts EVERY time you call it...so lets only count

// once, then save the value

len = strlen(str);

// fgets() will include the '\n'...lets get rid of it.

if ( str[len-1] == '\n' ) {

str[len-1] = '\0';

// And update our saved length

len--;

}

printf("Your string is: \"%s\"\n", str);

// Now we reverse it. We start at the ends and

// work our way to the middle.

for ( i = 0; i < len/2; i++ ) {

tmp = str;

str = str[len-i-1];

str[len-i-1] = tmp;

}

return 0;

}

Link to comment
Share on other sites

Link to post
Share on other sites

This is a quick string reverse function :

void strrev(char * str){    if (str)    {        char * end = str + strlen(str) - 1;        while (str < end)        {            swap(str,end);            str++;            end--;        }    }}

where swap() is :

void swap(char *x, char *y){    char temp = *y;    *y = *x;    *x = temp;}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×