puts array

This is a discussion on puts array within the C Programming forums, part of the General Programming Boards category; Dear all I have written the following code to reverse an array. but it don't run! without any error or ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question puts array

    Dear all
    I have written the following code to reverse an array. but it don't run! without any error or warning. can anyone help me?

    Code:
    main()
    {
    	static char t[]="abcdefghijklmnopqrstuvwxyz";
    	char temp;
    	int i, length;
    	
    	length=sizeof(t);
    	
    	puts("\n the original text is: ");
    	puts(t);
    	for (i=0; i<length; i++)
    	{
    		length--;
    		temp=t[i];
    		t[i]=t[length];
    		t[length]=temp;
    	}
    	puts("\n the reversed text is: ");
    	puts(t);
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    include string.h and change sizeof to strlen.

    sizeof gives you the size of the pointer (4 bytes).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Cool

    Hi,

    Use the following program printing an array elements in an reversing order.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
            static char t[]="abcdefghijklmnopqrstuvwxyz";
            char temp;
            int i,length=0;
    
            printf("\nthe original texts are: ");
            printf("%s\n",t);
            int k;
            k=strlen(t);
    
            printf("the reversed texts are: ");
            for(;k>=0;k--)
            {
                    printf("%c",t[k]);
            }
    
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    meher81:

    Your program works if you change:

    Code:
    length=sizeof(t);
    to

    Code:
    length=strlen(t);

    don't forget to include string.h.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question

    I did that, but it did not work.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Cool

    Hi meher81,

    Whatever Subsonics asked to change in your code I used to change.
    Following code with the changes insteadof sizeof(t) I mentioned strlen(t) with string.h header file, for me it's working fine.

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
            static char t[]="abcdefghijklmnopqrstuvwxyz";
            char temp;
            int i, length;
    
            length=strlen(t);
    
            puts("\n the original text is: ");
            puts(t);
            for (i=0; i<length; i++)
            {
                    length--;
                    temp=t[i];
                    t[i]=t[length];
                    t[length]=temp;
            }
            puts("\n the reversed text is: ");
            puts(t);
    }
    After that also If you have problem in your code,show your code.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Unhappy

    Thank you rekha_sri

    I confuse!!

    I copy your code instead of mine. It compiled successfully, same as mine. but when I run it, nothing appear!

    may be this problem relates to my compiler?
    I use turbo c2.0

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Maybe you need an fflush(stdout) in there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question

    I don't have that header file.
    I can run other programs successfully!
    I really don't know what the reason is!!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by meher81 View Post
    I don't have that header file.
    You don't have string.h? Time to get a new compiler, or at least reinstall the one you have. Speaking of that, the one you have is like twenty years old.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Exclamation

    I don't have stdout.h not string.h

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    No one asked you to include stdout.h. The suggestion was for you to use fflush(stdout), in which case #include <stdio.h> should suffice.
    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

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    I included stdio.h file.
    it is not my problem.
    thank you

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    So, what is your current code? Are you running your programs from a command prompt?
    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

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You should have, and should include the header file:

    Code:
    #include <string.h>
    The file string.h, will be in your TC\include directory

    I use Turbo C/C++ ver. 1.01, a lot (the C compiler mostly), so I know you should have it. If you don't, then you need to re-download and re-install your compiler, because you don't have all the parts you'll need.

    It *is* your problem if you want to use strlen(), and many other C functions. You must have the right include files.

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

Similar Threads

  1. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 01:52 PM
  2. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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