Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C Program Help


ghost's Avatar
0 0

Alright, I'm trying to write a program that will take a specified substring out of a string. For example, if the string is testingonetwothree, the program would be able to create another string out of that string using specified parameters. The program I've wrote is…

#include <stdio.h>

char substring (char source[], int start, int count, char result[]) { int i, n;

for ( i = start + 1, n = 0; i &lt; start + 1 + count; ++i, ++n ) {
result[n] = count[i];
if ( count[i] == &#39;&#92;0&#39; )
return result;
}
result[i] = &#39;&#92;0&#39;;
return result;

}

int main (void) { char substring (char source[], int start, int count, char result[]);

int i;

char string[] = "Testingonetwothree"; char stwo[81];

substring (string, 3, 4, stwo); printf ("%s is now %s\n", string, stwo ); return 0; }

substring.c: In function 'substring': substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast

Also I'm not sure if it's correct to try to store the new char array as I have it, or to set the result array = to the function which will return the desired string. Sorry if that doesn't make sense.


ghost's Avatar
0 0

Is this what you are looking for?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

char *substring(char *source, int start, int count)
{
[tab]char *result = (char *)malloc( count-start+1 );
[tab]int i;

[tab]for (i=0; start &lt; count; start++, i++)
[tab]{
[tab][tab]result[i] = source[start];
[tab][tab]if (result[i] == &#39;&#92;0&#39;)
[tab][tab][tab]return result;
[tab]}
[tab]result[i++] = &#39;&#92;0&#39;;
[tab]return result;
[tab]
}

int main()
{
[tab]char string[] = &quot;Testingonetwothree&quot;;
[tab]char *stwo;

[tab]stwo = substring( string, 3, 4);
[tab]printf(&quot;&#92;&#39;%s&#92;&#39; is now &#92;&#39;%s&#92;&#39;&#92;n&quot;, string, stwo);
[tab]free(stwo);
[tab]return 0;
}

ghost's Avatar
0 0

Yeah, that would probably work. While I could use pointers, I got this challenge in a book before pointers were introduced. Pointers is the chapter I'm reading now, but I would like to write it without them, since that was the intent.


ghost's Avatar
0 0

Deflogc wrote: Alright, I'm trying to write a program that will take a specified substring out of a string. For example, if the string is testingonetwothree, the program would be able to create another string out of that string using specified parameters. The program I've wrote is…

#include <stdio.h>

char substring (char source[], int start, int count, char result[]) { int i, n;

for ( i = start + 1, n = 0; i &lt; start + 1 + count; ++i, ++n ) {
result[n] = count[i];
if ( count[i] == &#39;&#92;0&#39; )
return result;
}
result[i] = &#39;&#92;0&#39;;
return result;

}

int main (void) { char substring (char source[], int start, int count, char result[]);

int i;

char string[] = "Testingonetwothree"; char stwo[81];

substring (string, 3, 4, stwo); printf ("%s is now %s\n", string, stwo ); return 0; }

substring.c: In function 'substring': substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast

Also I'm not sure if it's correct to try to store the new char array as I have it, or to set the result array = to the function which will return the desired string. Sorry if that doesn't make sense.

for ( i = start + 1, n = 0; i &lt; start + 1 + count; ++i, ++n ) {

looks like shit.. try splitting it into 2 for loops (1 being nested) like

{
     for( n=0; yadaydad)
    {
         do shit here;
    }
}```

also change count[i] to source[count], thats all i can say after 10 sec look at the code, good luck anyway

EDIT: code doesn&#39;t keep indents? pos

spyware's Avatar
Banned
0 0

Yeah. Nested loops. That'll improve the quality of the code, and speed up execution time. Good thinking!


ghost's Avatar
0 0

spyware wrote: Yeah. Nested loops. That'll improve the quality of the code, and speed up execution time. Good thinking!

where does speed of execution come into this thread? he wasn't looking for optimised code. only a minority of coders need optimised code now, and it doesn't look like he'll be coding anything that level just yet.

Since when are nested loops considered poor quality?

it would be nice for constructive critisism(EDIT: (sp?) ) than a mindless cunt flame kthx


ynori7's Avatar
Future Emperor of Earth
0 0

substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer Those errors are because you are using array notation on count, which is not an array, it's an integer. substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast And these errors are because you are returning "result", where it should be "result[]".

I dont know if there are any semantics errors in there (probably are) because I dont have time to read through it, but those are the reasons for the errors you posted.


ghost's Avatar
0 0

ynori7 thanks, that was the problem and it now works how I intended. Sorry for wasting your guys time with such dumb mistakes, but sometimes you get tired at looking at the same thing and not seeing an obvious error. The help is greatly appreciated.