Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first word

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Is there a specific code for MS C++ 6.0 to find the length of the FIRST word in ANY string.
Ex: (This is my sentence)

The length of the first word is 4.

Thank you
 
I don't know of any specific function to do this but you could always test for the first 'space' character using a counter, eg:

char* myString = "Hello world!";
char spaceChr = ' ';

int x = 0;

while (myString[x] && myString[x] != ' ')
{
x++;
}

// The length of first word is value of 'x'.
 
Hi,

The code below finds the length for the first word, but i'm not sure if it is what you asked for as mentioned "specific code for MS C++ 6.0".

char *x = "This is my sentence";
int c = 0;
char v;
const char s = ' ';

int t = strlen( x );

for( int i = 0 ; i < t; i++ )
{
v = x[ i ];

if( strncmp( &v , &s, 1) != 0 )
c++;
else
break;
}


Anyway, i hope helps at least a little.
 
hi,

refer to this code.

CString test = &quot;This is my sentence&quot;;
int nFind = 0;
nFind = test.FindOneOf(&quot; &quot;);

use value of nFind... ^^;


 
how bout just

int val = strstr(&quot;This is the sentence&quot;,&quot; &quot;);

val is the index of the first space, which is 4, which is the lenght of the first word.

Matt
 
Here is a complete and very simple solution to your problem:

#include <stdio.h>
#include <string.h>
void main()
{
char string[100];
int i = 0;
printf(&quot;Enter a string:\n&quot;);
gets( string );
while( string != ' ') // This is the part you need
i++;
printf(&quot;The lengh of the first word is: %d\n&quot;,i);
}

I hope that this will be helpful !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top