C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 4th, 2012, 07:23 PM
ProLearning ProLearning is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 ProLearning User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old November 4th, 2012, 07:55 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 6 h 15 m 15 sec
Reputation Power: 2194
Code:
*result += '\0';


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!

Reply With Quote
  #3  
Old November 4th, 2012, 07:58 PM
ProLearning ProLearning is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 ProLearning User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old November 5th, 2012, 12:53 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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));
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to obtain substring from full string? Please Look.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap