Improve company productivity with a Business Account.Sign Up

x
?
Solved

Function that returns a string

Posted on 2000-04-28
6
Medium Priority
?
175 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 160 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
Building an Effective Phishing Protection Program

Join Director of Product Management Todd OBoyle on April 26th as he covers the key elements of a phishing protection program. Whether you’re an old hat at phishing education or considering starting a program -- we'll discuss critical components that should be in any program.

 

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

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.

595 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