Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have the string which user insert

i want to take that string and convert it to array


then check the first word in that array(first element)
also i want to get text after certain word


example of what i want


user entered the following statement

"jack play football in the garden "


i want to

1- get (jack) to check it
2- want string after football (in)

how to achieve that note that user entered string sperated by sapce
Posted
Comments
nv3 2-Nov-14 19:11pm    
1. Input the string into an array of char
2. Define an array of pointers to char (for pointers to the start of the words)
2. Tokenize the string, for example by using strtok
While doing that:
2a. Assign a pointer to the begin of a new word to the next array element
2b. Replace the intermediate space character by a Null character, thereby
terminating the word as a separate string
3. Access the pointers in your pointer array to get to arbitrary words.
TheSniper105 2-Nov-14 20:30pm    
so how to access for example the third word can you provide example ?
syed shanu 2-Nov-14 21:25pm    
Hi,
Try like split the string by space and add all the splited string to array ,From that you can get the 3rd word of array which you need.Hope this will help you
syed shanu 2-Nov-14 21:27pm    
To split string and add to Array chk this links
http://stackoverflow.com/questions/23125357/how-to-split-a-c-string-into-an-array-of-c-strings

http://www.dotnetperls.com/split
http://stackoverflow.com/questions/11198604/c-split-string-into-an-array-of-strings

1 solution

Hi,

For this requirements you need to create a char pointer and store the string from user input to the pointer and then travel through the string...

For reading the string use gets function..

Travelling through the string will help you to find out each letter and also the required word..

Arrays can be used to store the words but you are not sure about how many size each muti dimensional arrays holds.


its better to use Char pointer instead of array..

Thanks,
Ullas
 
Share this answer
 
v2
Comments
TheSniper105 3-Nov-14 19:45pm    
how to make sure for example that jack will be the first first word if user entered space before it (i always enter jack in every string first word i.e jack is constant )
Ullas_Krishnan 3-Nov-14 20:32pm    
Hi,

You can just check the first n character of string is "Jack" or something else by travelling through the user input string...

Or this can be achieved by strcmp function... by extracting first 5 character from use input string.
After getting the string its easy for you to check does the first character is space or character by ASCI value or by "".
Thanks,
Ullas

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