user9276887
user9276887

Reputation:

C program returning random characters at end of program? (last function)

This code is just me messing around with pointers and seeing their use as I'm a beginner. I ran into a problem towards the end of my code though. It's supposed to display each word on a new line however after the last word it just displays a random set of characters.

#include <stdio.h>
#include <string.h>

void printWord(char *start); 
char *nextWord(char *start); 


 void main() 
 {
     int i = 0;
     char location[200] = "7825 CREEK VALLEY SACRAMENTO 95828 CA";
     char *ptr;
     ...

First Function prints the first 3 words

// 3. instead of printing characters (using putchar) until a '\0', printWord 
//    prints characters until a space ' '
puts("First Word");
printWord(location);

puts("Second Word");
printWord(location+5);

puts("Third Word");
printWord(location+11);
puts("");

The second function is the one causing me problems, the comments explain it as best as I can.

    ...
    // starting from the first character in the input string, each call to 
    // "nextWord" should return the next word in the string
    // e.g. if the input string is "Hi there everyone" :
    // first call to nextWord should return the address of the letter 't' of 
    //  "there"
    // second call to nextWord should return the address of the first letter 
    // 'e'of "everyone"
    // third call to nextWord should return NULL

    ptr = location;
    while(ptr)
    {
        // instead of printing characters (using putchar) until a '\0', 
        // printWord prints characters until a space ' '
        printWord(ptr);
        printf("\n");
        ptr = nextWord(ptr);
    }


}

This function works as it should

void printWord(char *start)
{
    for(; *start != ' '; start++){
        putchar(*start);
    } 
    putchar('\n');
}

However this one is causing me problems...

char *nextWord(char *start)
{
    int i=0;
    for(; *start != '\n'; start++){
        if(*start == ' '){return(start+1);}
        else if(*start == '\0'){return NULL; }

    }
}

Upvotes: 2

Views: 101

Answers (2)

David
David

Reputation: 8318

just edit the printWord() function as follows:

void printWord(char *start)
{
    for (; *start != ' '; start++) {
        if (*start == '\0') { break; }
        putchar(*start);
    }
    putchar('\n');
}

you didn't handle the end of line case

Upvotes: 1

Benjamin Barrois
Benjamin Barrois

Reputation: 2686

In your code:

char *nextWord(char *start)
{
    int i=0;
    for(; *start != '\n'; start++){
        if(*start == ' '){return(start+1);}
        else if(*start == '\0'){return NULL; }

    }
}

the test condition in your for is wrong. Your words are delimited by a space in your original character string. Try:

char *nextWord(char *start)
{
    tmp = start;
    while( (*tmp != ' ') && (*tmp != '\0')) {
       tmp++;
    }
    return (*tmp == '\0' ? NULL : tmp+1);
}

Moreover, in your example, you are modifying the pointer start which is in parameter, which is not useful as you are returning the pointer value.

Upvotes: 0

Related Questions