confused with question from book

This is a discussion on confused with question from book within the C Programming forums, part of the General Programming Boards category; this question is in a book im working from (which i have copied word for word): Re-write the program ex_06.c ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    confused with question from book

    this question is in a book im working from (which i have copied word for word):

    Re-write the program ex_06.c found in chapter 9 on one-dimensional arrays, to encompass a function with the following prototype.

    Code:
    char *to_upper(char *array);
    where the function to_upper converts any lower-case letters found in the array and returns a pointer to a string containing upper-case characters. The advantage of this function is that the contents of the original array need not be changed.

    chapter9_ex_06.c

    original program written by author:
    Code:
    #include <stdio.h>
    #define to_upper(c) ('a'<= (c) && (c) <= 'z' ? (c)-32: (c))
    
    main()
    {
             const char null = 0;
                       char text[255];
                       int   index;
    
             printf("input a line of text\n");
             gets(text);
    
              for (index = 0; text[index] != null; index++)
                        text[index] = to_upper(text[index]);
    
               printf("capitalized text\n%s", text);
    }

    my program in answer to question:

    Code:
    #include <stdio.h>
    
    
    char *to_upper(char *array)
    {
    
    	int i = 0;
    	
    	while (array[i]){
    		if ((array[i] >= 'a') && (array[i] <= 'z'))
    			 array[i] = (array[i] - 32);
    			 i++;
    	}
    	
    	return array;
    }		
    
    main()
    {
    	
    		  char input[25];
    		  
    	
    	printf("input a line of text\n");
    	gets(input);
    	
    	to_upper(input);
    	
    	
    		
    	printf("capitalized text\n%s", input);
    	
    }
    it works but not how the question requests,
    my version works without returning a pointer as the "array" gets passed by reference and thus gets changed in the function to_upper so not needing a return at all. A lso i must be missing something else because the author mentions that the contents of the original array need not be changed, my version always changes the original array. I have been reading about pointers but still dont understand this one.

    thanks

    luigi
    Last edited by luigi40; 07-22-2004 at 06:05 AM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    The only way not to change the original array and not sending another parameter is to allocate memory for a new string and return that pointer.

    Code:
    char *to_upper(char *array)
    {
        int len = strlen(array);
        char *str = (char*)malloc(sizeof(char) * (len + 1));
        int i;
    
    	for(i = 0; i < len + 1; i++)
    		str[i] = ((array [i] >= 'a') && (array [i] <= 'z')) ? array [i] - 32 : array[i];
    
    	return str ;
    }
    But dont forget to free the memory when your done.
    Last edited by erikj; 07-22-2004 at 06:30 AM.

  3. #3
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by erikj
    Code:
    char *to_upper(char *array)
    {
        int len = strlen(array);
        char *str = (char*)malloc(sizeof(char) * (len + 1));
    But dont forget to free the memory when your done.
    Yes, and bare in mind that:
    • strlen() needs <string.h>
    • the type returned by strlen() is size_t
    • malloc() needs <stdlib.h>
    • sizeof (char) is 1 by-definition.
    • casting the return of malloc() is not necessary.
    • malloc() can fail at any time. The returned value should be tested. NULL means 'allocation failed for any reason'
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I agree with Emmanuel but have something to add
    1) It is good practice to declare function first and then define it.
    2)Use standard C
    Replace main(){...} with int main(){...} read FAQ
    Although char is one byte on most machines it is good practice to use sizeof
    number of using malloc and free should be equal in program
    I strongly recommend reading FAQ.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Although char is one byte on most machines it is good practice to use sizeof
    By the standard sizeof(char) WILL be 1. What can change is the number of bits per byte but sizeof(char) is 1.

    1) It is good practice to declare function first and then define it.
    It depends really. If I'm writing a general function for usage among different programs, I'll put it in it's own source file that has the function at the beginning and a test main()below it. Once I'm done testing the function I'll comment out the main().
    Last edited by Thantos; 07-22-2004 at 02:37 PM.

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Micko
    I agree with Emmanuel but have something to add
    1) It is good practice to declare function first and then define it.
    You mean a separated prototype? The place for a separated prototype is a header for functions with external linkage (public).

    If the function is private (static) I recommend to arrange the layout so that a static function is defined before its use. That way, weird things like mutual call (actually hidden recursion) appears clearly.

    Is mutual call good design or not is another issue.
    2)Use standard C
    Replace main(){...} with int main(){...} read FAQ
    Although char is one byte on most machines it is good practice to use sizeof
    No. According to the definition of the C-language, the size of an object is expressed in number of bytes. A byte has exactly the size of a char. Like any unit, its value is one (whatever the implementation). Hence sizeof (char) equals 1 byte. Always.

    What may change is the range of a byte, but it has no incidence on the fact that the sizeof of a char (or byte) is 1.

    number of using malloc and free should be equal in program
    I strongly recommend reading FAQ.
    Naive calculation. How do you count realloc()?
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Thantos
    It depends really. If I'm writing a general function for usage among different programs, I'll put it in it's own source file that has the function at the beginning and a test main()below it. Once I'm done testing the function I'll comment out the main().
    I confirm that this is definitely a good practice.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Emmanuel Delaha
    Quote Originally Posted by Thantos
    By the standard sizeof(char) WILL be 1. What can change is the number of bits per byte but sizeof(char) is 1.

    It depends really. If I'm writing a general function for usage among different programs, I'll put it in it's own source file that has the function at the beginning and a test main()below it. Once I'm done testing the function I'll comment out the main().
    I confirm that this is definitely a good practice.
    I do the same, but I tend to do:
    Code:
    #ifdef FILENAME_STANDALONE
    int main ...
    {
        ...test here...
    }
    #endif
    And compile with:

    gcc -o filename filename.c -DFILENAME_STANDALONE

    Or something to that effect.

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

  9. #9
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    wow you guys have gone over my head a little, however i much appreciate your feedback and will peruse your input.

    thanks

    luigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Question about book
    By Kaidao in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 02:31 AM
  3. Can't display book and borrower's details.
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2003, 10:18 AM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM
  5. Book Question
    By Drew in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-17-2001, 06:58 PM

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