Thread: My understanding of pointing is so disappointing: Exercise 5b Chapter 13

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    My understanding of pointing is so disappointing: Exercise 5b Chapter 13

    Hey Guys, I'm working on what for me has been the best chapter so far in this book and it is kicking my butt but, I'm learning a ton. This code finds letters in a string and capitalizes them. It skips what it has to. The updated string is copied into a char array called 'new'.

    Can someone please help me understand why I can't print out the first letter or the entire string at the end of this code?

    BTW: I have to use the pointer arithmetic.

    Code:
    #include <stdio.h>
    
    void capitalize(char str[], int n);
    int n=20;
    
    int main(){
        char ch[]="ljS4e8sjHd67ds";
        int size = sizeof(ch)/sizeof(ch[0]);
        capitalize(ch, size);
        return (0);
    }
    
    void capitalize(char str[], int n){
        printf("%s\n",str);         // prints entire string
        char new[n];
        char *p;
        p=str;
        printf("p=%s\n",p);         // prints entire string
        for(p=str; p<str+n; p++){
            if(*p>64 && *p<91){
                *new=*p;
            }else if(*p>96 && *p<123){
                *new=toupper(*p);
            }else{
                *new=*p;
            }
            printf("%c\n", *new); // *q and *new both work
        }
        printf("%c", new[0]);   // not working: I expected L
        printf("%s", new);      // not working: I expected LJS4E8SJHD67DS
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    You are all the time overwriting the first element in your "new" array. The last character you're writing is the terminating null from the original string. Thus, the resulting string in "new" is a zero-length string.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Oh my gosh. This is so obvious now that you pointed this out. Thank you so much. My code now works.

    Code:
    #include <stdio.h>
    // http://knking.com/books/c2/answers/c13.html
    
    
    void capitalize(char str[], int n);
    int n=20;
    
    
    int main(){
        char ch[]="ljS4e8sjHd67ds";
        int size = sizeof(ch)/sizeof(ch[0]);
        capitalize(ch, size);
        return (0);
    }
    
    
    void capitalize(char str[], int n){
        char new[n];
        int i=0;
        char *p;
        p=str;
        for(p=str; p<str+n; p++){
            if(*p>96 && *p<123){
                new[i]=toupper(*p);
            }else{
                new[i]=*p;
            }
            i++;
        }
        printf("%s", new);      // working as expected LJS4E8SJHD67DS
    }

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    Now that you're fiddling with pointers, what about using a pointer variable to fill your "new" array? Hint: The array designator is also a pointer to the first element of the array. That's the reason why the indirection operator (*) worked in your first attempt.

    Unrelated side notes:
    - The n parameter of your capitalize() function shadows the (unused) global n variable.
    - One day you may come across the term Variable-length array - Wikipedia. The more you read about it the less you probably want to use VLAs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2016, 05:50 AM
  2. Replies: 3
    Last Post: 08-04-2016, 05:35 AM
  3. Jumping into C++ Chapter 14, exercise 6.
    By CppProgrammer88 in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2016, 07:27 PM
  4. Alex's exercise Chapter 6 Ex3
    By Valentas in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2013, 10:55 AM

Tags for this Thread