The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to obtain substring from full string? Please Look.
Discuss How to obtain substring from full string? Please Look. in the C Programming forum on Dev Shed. How to obtain substring from full string? Please Look. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 4th, 2012, 07:23 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
|
|
|
How to obtain substring from full string? Please Look.
There are many limitations placed for this code. I am still learning so I need hints for this assignment.
No extra variables, no macros or functions other than GetSubstring can be called.
Main function cannot be changed.
This is my code. Focus on the GetSubstring function part.
#include <stdio.h>
#include <stdlib.h>
char *GetSubstring(const char source[], int start, int count, char result[]);
int main(void)
{
const char source[] = "one two three";
char result[] = "123456789012345678";
puts(GetSubstring("This is really fun", 2, 800, result));
puts(GetSubstring("This is really fun", 261, 9, result));
puts(GetSubstring("This is really fun", 0, 12, result));
puts(GetSubstring(source, 5, 87, result));
puts(GetSubstring(source, 18, 7, result));
puts(GetSubstring(source, 6, 5, result));
puts(GetSubstring(source, 0, 3, result));
return(EXIT_SUCCESS);
}
char *GetSubstring(const char source[], int start, int count, char result[])
{
char *copy = result;
for (; *source != '\0' || *source == source[start]; start--)
{
while (*source != '\0')
{
*result++ = *source++;
}
}
*result += '\0';
return (copy);
}
Current Output is:
This is really fun
This is really fun
This is really fun
one two threey fun
one two threey fun
one two threey fun
one two threey fun
Process returned 0 (0x0) execution time : 0.082 s
Press any key to continue.
|

November 4th, 2012, 07:55 PM
|
|
|
Can't do this with C strings.
result is already to pointing to where you need to put the null terminator, so just use =.
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

November 4th, 2012, 07:58 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
|
|
|
right.
Thanks for that I'm trying everything I can to change things around
|

November 5th, 2012, 12:53 AM
|
 |
Contributed User
|
|
|
|
1. GetSubstring doesn't use the count parameter. Use it, or remove it.
2. Massive out of range values
puts(GetSubstring("This is really fun", 2, 800, result));
puts(GetSubstring("This is really fun", 261, 9, result));
If you use these to index your short strings, then you have undefined behaviour in your code.
3. When you post code, make sure you put [code][/code] tags around the code. It should look like this.
Code:
int main(void)
{
const char source[] = "one two three";
char result[] = "123456789012345678";
puts(GetSubstring("This is really fun", 2, 800, result));
puts(GetSubstring("This is really fun", 261, 9, result));
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|