Thread: Character string

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Character string

    I'm working through an example from Kochan C programming and stuck on this exercise.

    The exercise has us writing a function that will display the n'th charater from a string, for an specific amount of characters after the n'th character. So i've built this and it works when in the for loop I don't have (string[i]='\0'), but as soon as I have that in the for loop, the remain of the string is return and not just the # of character asked for. The reason I added that (string[i]='\0'), is that if the number of characters asked for exceed the end of the string, only what is avalible is to be returned.

    Code:
    #include <stdio.h>
    
    // Prototype Declaration
    
    void sub(const char string[],int start, int count, char answer[]);
    
    int main (void){
       char string[]="character";
       char answer[80]="'\0'";
       int start=5;
       int count=2;
        
        sub(string,start,count,answer);
        printf("%s",answer);
        
    return 0;
    }
    void sub(const char string[],int start, int count, char answer[]){
        
       int j= 0;
       for(int i=start; ((i < (start+count)) || (string[i]!='\0')); ++i){
           answer[j]=string[i];
           j++;
       }
        
    }
    Why does it not work, when I add (string[i]!='\0')?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check your logic. Speak it out loud in english to understand what you're telling the program to do.

    Code:
    for(int i=start; ((i < (start+count)) || (string[i]!='\0')); ++i)

    "The loop will continue executing if 'i' is less than 'start' + 'count' *** OR *** the current character in string is not a null character."

    Based on the values in your program - when the first argument is false (which, by itself, would exit the loop), the second argument is still still true (the current character in the string is not a null character), and it keeps on printing until the end of the word.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    of course, thank you. that makes sense now, made string[i]=='\0' and it worked.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It might work with the current values, but it is still not correct.

    "The loop will continue executing if 'i' is less than 'start' + 'count' *** OR *** the current character in string is a null character."

    With this logic, you will still overrun your array if 'start' + 'count' is greater than the array size. In fact, if the current character is a null character, this logic guarantees the loop will keep going.

    Your initial code was more close to correct.

    Here's a hint:
    - You have two conditions.
    - You only want the loop to continue if both are true
    - Or, put another way, if either condition is false, you want to break the loop
    - If you want the loop to break if either condition is false, use logic AND.

    Code:
    X        Y        X AND Y
    -------------------------
    FALSE    FALSE    FALSE
    FALSE    TRUE     FALSE
    TRUE     FALSE    FALSE
    TRUE     TRUE     TRUE

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Okay starting to make sense (thank you for taking a pedagogical approach - much better than just getting an answer). So if using OR as long an one of the conditions is true, the condition is met, whereas using AND, both conditions must be true for it to be met. So in this case, the condition is that i<(start+count) as well as cannot be a null char.

    thx

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, that's it! And a thank you to you, too. It is immensely gratifying when someone thinks about the ideas presented to them, and learns from it. Happy coding!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Next character in a string?
    By Bnorman in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2009, 08:42 AM
  2. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  3. Replies: 3
    Last Post: 11-03-2002, 02:14 AM
  4. How to character in specified string !!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-17-2002, 09:17 AM
  5. character in string
    By canine in forum C Programming
    Replies: 6
    Last Post: 01-24-2002, 12:28 PM