LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 05-14-2024, 07:34 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206

Quote:
Originally Posted by rtmistler View Post
Write a C program, using no library calls, allocating no memory, using no temporary holding variables, to reverse a string in place.
That had never crossed my mind! As the OP has not returned but the thread remains of interest, my feeble attempt...

Code:
/* Reverse string argv[1] in place with no lib call, no allocation, no temp vars */
int main(int argc, char **argv)
{
        if(argc<2)
                return 0;
        for(argc=0; *(argv[1]+argc); argc++)
                ;
        *(argv[0])=0;
        while(*(argv[0])<--argc)
                {
                *(argv[1]+*(argv[0]))^=*(argv[1]+argc);
                *(argv[1]+argc)^=*(argv[1]+*(argv[0]));
                *(argv[1]+*(argv[0]))^=*(argv[1]+argc);
                ++*(argv[0]);
                }
        return 0;

}
Use a debugger to show the result as suggested by rtmistler (or add the stdio lib for printf()).

Should check if the string is longer than can be indexed by char, but...

UPDATE: Following is same but more readable, includes overflow guard, useful exit values...

Code:
/* Reverse string in place, no lib calls, allocs or tmp vars */
#include <limits.h>

#define HEAD *argv[0]
#define TAIL argc
#define SPTR argv[1]
#define NO_STRING_ERR 1
#define TOO_LONG_ERR 2

int main(int argc, char **argv)
{
        if(argc<2)
                return NO_STRING_ERR;
        for(TAIL=0; *(SPTR+TAIL); ++TAIL)
                ;
        if(TAIL/2 > CHAR_MAX)
                return TOO_LONG_ERR;
        HEAD=0;
        while(HEAD<--TAIL)
                {
                *(SPTR+HEAD) ^= *(SPTR+TAIL);
                *(SPTR+TAIL) ^= *(SPTR+HEAD);
                *(SPTR+HEAD) ^= *(SPTR+TAIL);
                ++HEAD;
                }
        return 0;
}

Last edited by astrogeek; 05-15-2024 at 12:11 PM. Reason: Use XOR swap, fewer lines, diddles, readable examp
 
Old 05-15-2024, 05:56 AM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by astrogeek View Post
That had never crossed my mind! As the OP has not returned but the thread remains of interest, my feeble attempt...
I noticed last night it isn't in Programming, asked for it to be moved. Sadly the OP may be a brief hit and run poster. Good stuff!

If people have written bare metal, no OS code, these are the types of things we deal with in embedded all the time.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Fuduntu Programming Challenge LXer Syndicated Linux News 0 03-31-2011 10:11 PM
An esoteric programming challenge Bert Programming 0 03-22-2002 07:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 01:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration