string reversal

This is a discussion on string reversal within the C Programming forums, part of the General Programming Boards category; Originally Posted by WatchTower You need return 0 in your main function. It's optional according to latest C standards. Originally ...

  1. #16
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by WatchTower View Post
    You need return 0 in your main function.
    It's optional according to latest C standards.
    Quote Originally Posted by Adak View Post
    Perhaps it doesn't run on your compiler, but it runs just fine on mine (Turbo C).
    Might be because it's an old compiler.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by Adak
    Perhaps it doesn't run on your compiler, but it runs just fine on mine (Turbo C).
    I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by laserlight View Post
    I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.

    I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors. Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.

    The resulting program runs correctly. Some sample output appropriate for July 4th:



    Hoorah for the flag of the free!
    May it wave as our standard forever
    The gem of the land and the sea
    The banner of the right.
    Let tyrants remember the day
    when our fathers with mighty endeavor,
    proclaimed as they marched to the quay
    that by their might and by their right
    it waves forever...


    !eerf eht fo galf eht rof harooH
    reverof dradnats ruo sa evaw ti yaM
    aes eht dna dnal eht fo meg ehT
    .thgir eht fo rennab ehT
    yad eht rebmemer stnaryt teL
    ,rovaedne ythgim htiw srehtaf ruo nehw
    yauq eht ot dehcram yeht sa demialcorp
    thgir rieht yb dna thgim rieht yb taht
    ...reverof sevaw ti


    Press Enter When Ready

  4. #19
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.
    Now after searching the net I get to know that string literals can't be modified as they ar stored in read-only section of the memory by the OS. But my question is why string literals are given such type of a special "treatment"?
    And by the way is anything between double quotes a string literal?

    Quote Originally Posted by Adak View Post
    I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors. Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.

    The resulting program runs correctly. Some sample output appropriate for July 4th:
    Maybe the last line of this FAQ is helpful here.
    C-FAQ
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #20
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    Hi,

    I have a doubt about string reversal. The code below reverses the string character by character but when i give it to print out the result there occurs an error. I am unable to figure out where am i going wrong.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void xstrrev(char *);
    
    int main(){
    
    	int i;
    	char *s[] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    
    	for(i=0;i<3;i++){
    		xstrrev(s[i]);
    	}
    }
    
    
    void xstrrev(char *a){
    
    
    	int len,j;
    	char *p;
    
    	printf("\n\r%s", a);
    	len = strlen(a);
    	printf("\n\r%d", len);
    	p = malloc(len+1);
    
    	for(j = 0;j<=len;j++){	
    		p[j] = a[len-j];
    		printf("\n\r%c%c", a[len-j], p[j]);
    		if(j == len)
    			printf("\n\r%s", p);
    	} }
    After going through the details of arrays of pointers and character strings(and also hijacking your thread) I made 3 solutions of this question. But in all of them the main thing is that I'm swapping the characters of each string until the mid-character. Out of the three approaches I took one of them was with malloc which is similar to your code posted. But I dont understand what you're trying to do in the colored lines. What I've done is after assigning the storage to p, copied a into p and then the same swapping thing. This way the swapped string gets stored in p.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by Adak
    I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors.
    That is, the compiler was able to compile the code without warnings or errors. That is different from saying that the program does not run (but admittedly I see the potential for confusion since a program that does not compile obviously does not run: perhaps BEN10 should have mentioned a run time error or something).

    Quote Originally Posted by Adak
    Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.
    Haha. That is not the sign of a "smart compiler", since a compiler (or linker/loader invoked by the compiler) might link to the C standard library by default, and it is possible for a function to be used without being declared at the point of usage. One possible reason that you did not get a warning is that <string.h> could have been included by a header that you did include - relying on this would be relying on something implementation specific. Another possible reason is that the warning level was not high enough.

    Quote Originally Posted by Adak
    The resulting program runs correctly. Some sample output appropriate for July 4th:
    As such, you are observing the effect of undefined behaviour. It appears to work in your case, but is nonetheless incorrect.

    Quote Originally Posted by BEN10
    But my question is why string literals are given such type of a special "treatment"?
    I believe there are practical reasons related to how implementations might implement string literals, but consider this: what does it mean to modify a literal? For example, does this make sense to you?
    Code:
    1 = 2;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks everybody. Now I think some of the loops in my mind are opening regarding pointers.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21