How would u determine the end of a word using pointers?

How would u determine the end of a word using pointers?

Here's an approach:

  1. First you read away all whitespaces.
  2. Then you read until you come across a non-alphabet character.
  3. Now you stop because you've reached the end of a word.

Solution:1
-------------
If you have strings consisting of many words like
"abc def xyz"
use strtok functions (space as a delimeter)
to get the words.
limitation:if multiple spaces occur like abc def xyz "
it wont work perfectly.
Solution:2
-------------
1)read the string charecter wise (one char at a time) until you find a space.
here is your word.
2)Increase the poiner until u get and alphabet again for going to the beginning of the next word. then again step:1.

1)read the string charecter wise (one char at a time) until you find a space.
here is your word.

Won't work if the string starts with spaces, what about a string like this: " firstword" .
However, it will work if you first skip all whitespaces.

Won't work if the string starts with spaces, what about a string like this: " firstword" .
However, it will work if you first skip all whitespaces.

so from the solution:2 of mine..
can we start with step-2 then go to step 1 and so on..?

Well, if you first skip all non-alphabetic characters, using a loop, then the loop will stop when the first letter of the word is encountered.
And then he'll have to run another loop, to get to the end of the word.

Edit:: So my first post was not correct.

thanks but u guys please check this code and find the bug cos i think this was supposed to work, bt it didnt.

main()
{
      char name[10];
      char *ptr;
      ptr = name;
      int i;
      puts ("Enter name: \n");
      scanf("%s",&name);
      
      for (i = 0; *(ptr+i) == '\0'; i++)
                       *ptr = '-';
                   printf("\n\n%s", ptr[i]);
      
 }

I guess you wanted to write this:
for (i = 0; *(ptr+i) [B][COLOR="Green"]!=[/COLOR][/B] '\0'; i++)[COLOR="Green"][B];[/B][/COLOR] (also note the semicolon at the end)

And this won't work either if you want to place a '-' at the end of the name:
*ptr = '-';.
I guess you wanted to write: *(ptr+i) = '-'; here.
And you'll also want to add this line: *(ptr+i+1) = '\0';right after it (because you overwrite the null-terminator with the previous line).

And you'll also want to change this:
printf("\n\n%s", ptr[i]);
to this:
printf("\n\n%s", ptr);
or better, to this:
printf("\n\n%s", name);

Remark: make sure that the string which will hold the name is big enough, maybe change its size to 20?

Why do you use a separate variable? If you could just increment the pointer?

Also: please post your code using code tags the next time

Edit:: I assumed that you wanted to place a '-' at the end of the string.
Edit:: Please mention the next time also what your code has to do as it will make life easier for both: you and me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.