Solved

Function that returns a string

Posted on 2000-04-28
6
159 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Goodbye, so long 10 92
How do I avoid pointer to integer casting errors in C programming? 4 192
valid enum? 6 69
Unable to start eclipse ? 17 87
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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now