Thread: Have a problem with the following code.

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    49

    Have a problem with the following code.

    Hi all,
    I know that there is something wrong with my code, can anyone pls help me to edit my code.
    What the code did was to take in a character and produce the reverse of that character.
    input --> hello world
    output --> dlrow olleh


    Code:
    #include <stdio.h>
    
    void reverse ();
    
    int
    main(void)
    {
         char s[100];
    
         while (fgets(s, 100, stdin) != NULL)
         {
    	  printf("%s\n", s);
    	  void reverse(int s);
    	  printf("String length == %d\n", strlen(s));
         }
    
         return 0;
    }
    
    void reverse(int s)
    { 
         int i;
         for(i=(sizeof(s)-1); i=0; i--)
        {
           printf("reverse string: %s", s);
        }
    }
    diana --> programming is tough

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, one problem is that you have a function prototype where you're trying to call reverse(). Another problem is that reverse() really wants a string, but you have it set up to receive an int.

    Really, doesn't your compiler give you error messages?
    If you understand what you're doing, you're not learning anything.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void reverse( char [] );
    
    int main(void)
    {
         char s[100];
    
         while ( fgets(s, 100, stdin) != NULL ) {
               printf("%s\n", s);
               printf("reverse string: ");
               reverse(s);
               printf("String length == %d\n", strlen(s));
         }
    
         return 0;
    }
    
    void reverse( char s[] )
    { 
         int i;
    
         for ( i = strlen(s) - 2; i >= 0; i-- ) {
    		 printf("%c", s[i]);
         }
         printf( "\n" );
    }

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    why do you want to use "-2" in the "for" loop.
    Code:
    for ( i = strlen(s) - 2; i >= 0; i-- ) {
    I try it with "-1" and it produce the same result...
    diana --> programming is tough

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    With -1 it probably prints a NULL first.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why do you want to use "-2" in the "for" loop.
    fgets copies the newline as well. - 1 for the null character, - 2 for the null character and the newline.
    My best code is written with the delete key.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    However, this assumes that there actually is a newline. There's no guarintee that there will be a newline. All I have to do is type 100 characters before hitting enter.

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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So if the user entered less than 100 characters, the -2 would work correctly. And if the user entered over 100 chars, fgets() would read in 100 chars, then -2 would chop off the last char (along with the null), thinking it was a newline?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >All I have to do is type 100 characters before hitting enter.
    You don't have to work nearly that hard. 99 characters should be enough to break it.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Ya.. got it, thanks everyone......
    diana --> programming is tough

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    [code]
    #include <stdio.h>
    #include <string.h>

    void reverse(char []);

    int
    main(void)
    {
    char s[100];

    while (fgets(s, 100, stdin) != NULL)
    {
    printf("%s", s);
    printf("reverse string:");
    reverse(s);
    printf("\nString length == %d\n", strlen(s));
    }

    return 0;
    }

    void reverse(char s[])
    {
    int i;
    for(i=(strlen(s)-1); i>=0; i--)
    {
    printf("%c", s[i]);
    }
    }
    [\code]

    This code did reverse the character input for it always have a '\n' after each character, is there any way that i can put all the reserved character and print them out all at once.

    Now when i compare and run the code, the output i get is

    Code:
    Enter a string of character: this
    String of character entered: this
    Reversed string: s
    Reversed string: i
    Reversed string: h
    Reversed string: t
    String length == 5
    How can i make it this way
    Code:
    Enter a string of character: this
    String of character entered: this
    Reversed string: siht
    Thanks...
    Last edited by dianazheng; 06-05-2005 at 01:04 AM.
    diana --> programming is tough

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    did any one get this problem when solving the above prob. i am confused. can any one correct me please

    Code:
    #include<stdio.h>
    
    void rev(char []);
    
    int main()
    {
        char str[25];
        
        while(fgets(str,sizeof str, stdin))
            rev(str);
        return 0;
    }    
    
    void rev(char temp[])
    {
        char temp2[25];
        int i,j;
        for(i=sizeof temp,j=0;temp[i]!='\0';i--,j++)
         temp2[j]=temp[i];
        
        temp2[j]='\0';
         printf("%s",temp2);
         return;
    }
    my output
    Code:
    hello
    olleþ@Ì \
    ol
    ol
    ol
    s.s.harish

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >did any one get this problem when solving the above prob
    Only those that wrote the same broken code as you.

    >i=sizeof temp
    temp is a pointer, so i would most likely be 2 or 4, not the size of str, and most certainly not the size of a variable length string entered by the user. This would work better:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void rev ( char str[] );
    
    int main ( void )
    {
      char str[25];
    
      while ( fgets ( str, sizeof str, stdin ) != NULL )
        rev ( str );
    
      return 0;
    }
    
    void rev ( char str[] )
    {
      int i = 0;
      int j = strlen ( str ) - 1;
      char temp[25];
    
      while ( j >= 0 )
        temp[i++] = str[j--];
      temp[i] = '\0';
    
      printf ( "%s\n", temp );
    }
    But since it's trivial to reverse a string in-place, you don't need the error prone temporary array:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void rev ( char str[] );
    
    int main ( void )
    {
      char str[25];
    
      while ( fgets ( str, sizeof str, stdin ) != NULL ) {
        rev ( str );
        printf ( "%s\n", str );
      }
    
      return 0;
    }
    
    void rev ( char str[] )
    {
      int i = 0;
      int j = strlen ( str ) - 1;
    
      while ( i < j ) {
        char save = str[i];
        str[i++] = str[--j];
        str[j] = save;
      }
    }
    Last edited by Prelude; 06-05-2005 at 09:29 AM.
    My best code is written with the delete key.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > for(i=sizeof temp
    Try strlen(temp)

    sizeof() simply tells you the size of a pointer (which is all you get when you pass an array to a function)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    problem is here

    Code:
     for(i=sizeof temp,j=0;temp[i]!='\0';i--,j++)
    it should be

    Code:
     for(i=(strlen(temp) - 1),j=0;i>0;i--,j++)
    you should use strlen() instead of sizeof(). temp is a char pointer. Its size is not known in function rev(). The size of the pointer is nothing but the sizeof the memory location address which is usually 4 bytes (32 bits).

    also the condition temp[i] != '\0' will always be true. Cause null termination char is seen only at the end of the string and not at the begining of the string. Since you are decrementing i, you are going from the end of the string to the begining of the string.

    And last but not the least, i should be initialized to strlen(temp) - 1.
    Cause in C array locations starts from 0. i.e. For array size of 5, array elements are located from 0 to 4 in the array. If the length of the string is 10 then actual characters in the string starts from 0 to 9. Char 10 is actually the null character. So u need to start one location before. (Or rather two locations becuase of new line character before null termination due to fgets()).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM