Solved

Function that returns a string

Posted on 2000-04-28
6
164 Views
Last Modified: 2010-04-15
Hi!

I'd like to compare the first words of some text lines. I wrote that functon, which should return  the pointer to the first word in every line (*result). In the example the function first_word is called twice, but it seems that the second call is executer before the first (it seems so from the output). I also don't understand why it's returned "The first word in these line is 123 and 123". Any ideas?


//--------------------------------------#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <conio.h>

char *result;

char* first_word(char *line)
{
int pos;
char *point=NULL;
point = strchr(line,' ');

if (point)
{   pos=point-line;
   printf("\nThe first space is at position: %d",pos);}
else
   { printf("No spaces"); return line; } // returns the whole line

   strncpy(result, line, pos);
   result[pos]='\0';
return result;
}

main(){

result=(char*)calloc(80,sizeof(char));
printf("\n The first word in these lines is %s and %s",first_word("123 WORD1"), first_word("4567 WORD2"));
getch();
}
//--------------------------------------
0
Comment
Question by:johnny_35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 40 total points
ID: 2759194
char* first_word(char *line)
{
    int pos
    char *point=NULL;
    point = strchr(line,' ');

    if (point) {
         pos=point-line;
                 printf("\nThe first space is at position: %d",pos);
    } else {
printf("No spaces");
return line; } // returns the whole line

     point = calloc (pos+1,sizeof(char));
    strncpy(point, line, pos);
    point[pos]='\0';
    return point;
     }
}
0
 
LVL 1

Expert Comment

by:zx9r
ID: 2761140
Well, there are some mistakes in your code. Try this:


/* includes stuff ... */

#define MAX_SIZE 100

char *first_word(char *line) {
   static char result[MAX_SIZE];
   int i = 0;

   while (line[i] && line[i] != ' ' && i < MAX_SIZE) {
      result[i] = line[i];
      i++
   }

   result[i] = 0;

   return (result);
   
}

main(){

   printf("\n The first word in these lines are %s and %s",first_word("123 WORD1"), first_word("4567 WORD2"));
getch();
}


I don´t compile it but it seems to work ;)
0
 
LVL 1

Expert Comment

by:zx9r
ID: 2761155
ehmm... well, i don´t try the code above  but better in main() try:

printf("\n The first word in these lines are %s",first_word("123 WORD1"));
printf("and %s\n", first_word("4567 WORD2"));


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:johnny_35
ID: 2761768
Great rbr, it works! May i ask what was wrong in my code? Just one little thing more...why is it shown the position of the first space in the second string before the one in the first string-like the second call was executed before the first??

Thanx
0
 
LVL 10

Expert Comment

by:rbr
ID: 2768402
Look at the C-FAQs
http://www.eskimo.com/~scs/C-faq/top.html

There is no guarantee in which order you r functions will be called. The output is undefined.
0
 

Author Comment

by:johnny_35
ID: 2768804
Undefined output???
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question