Solved

Function that returns a string

Posted on 2000-04-28
6
163 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

830 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