Solved

Function that returns a string

Posted on 2000-04-28
6
160 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

930 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

13 Experts available now in Live!

Get 1:1 Help Now