Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Anyone can help me to split a word into parts? For example the word 'searched' .
I just want to seperate 'search' in a string str1 and the other part 'ed' should be splitted in another string str2 . I think that I can use strcmp function in the library of string.h but I dont know how I should continue to solve the problem..

C++
int main()
{
        // Asked by Sabri Mevis at Gebze Institute of Technology

	char str[20];
	int i=0 , j =0 ;
	int length ;

	cout << "Write a word to be seperated : " ;
	cin >> str ;

	length = strlenFunk(str) ;

	if(strcmp(str, "true") ==0)
		cout << "You are on the right way.\n";

        // Here has not been written
	

	return 0 ;
}

int strlenFunk(const char *str)
{
       const char *chars;

       for (chars = str; *chars; ++chars) ;
       
       return (chars - str);
}
Posted
Updated 19-Oct-12 21:51pm
v2
Comments
Legor 22-Oct-12 4:59am    
Why dont you use std::string instead of char* if you're working with C++. It's much less errorprone and easier to use than the old C-style strings (char*).

See here for example:
http://www.cplusplus.com/reference/string/string/

1 solution

Please use strstr to find occurance of a sub string.

http://www.cplusplus.com/reference/clibrary/cstring/strstr/[^]

To split the second part, you can seek the pointer to the end of the first sub string

C++
char buff[200];
char part1[200];
char part2[200];

int nPart1Len = strlen("search");
int nPart2Len = strlen(buff) - nPart1Len;
strcpy( buff, "searched" );

if(strstr( buff, "search" )) // Check occurance of first string.
{
    memcpy( part1, buff, nPart1Len); //
    memcpy( part2, buff + strlen( "search" ), nPart2Len );

    part1[nPart1Len] = 0; // Set NULL at end of string.
    part2[nPart2Len] = 0; // Set NULL at end of string.
}
 
Share this answer
 
v3
Comments
[no name] 21-Oct-12 4:18am    
The code you gave above is only for two part. If I wanted to seperate for more parts may be 10 - 12 then what would I do? declaring many parts ?
Santhosh G_ 21-Oct-12 4:41am    
You can use memcpy with an offset for source parameter.

memcpy(buffn, actualstring +offset, ncurrentlength );
buffn is n th buffer, offset is the length of all strings from buff0 to buffn, ncurlength is the length of current string.
[no name] 21-Oct-12 7:06am    
I am not good enough at libraries of c++ ..Do you mind if you use it in a code please?
Santhosh G_ 21-Oct-12 18:10pm    
char cOutBuff[5][25]; // Buffer for 5 words.
char cInBuffer[250];
strcpy(cInBuffer,"AllPartsOfTheWord");
// Length of Words in an array
int nWordLengths[5] = {3,5,2,3,4};
int nOfset = 0;
for( int nI =0; nI<5;nI++)
{
// Copies part of a string.
memcpy(cOutBuff[nI], cInBuffer+nOfset, nWordLengths[nI]);
// Set Null at end of a string.
cOutBuff[nI][nWordLengths[nI]] = 0;

// Increment offset.
nOfset += nWordLengths[nI];
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900