Computing.Net > Forums > Programming > Please help me in C!

Please help me in C!

Reply to Message Icon

Original Message
Name: Brian
Date: November 11, 2003 at 02:04:24 Pacific
Subject: Please help me in C!
OS: Windows XP Pro
CPU/Ram: 2Ghz/256DDR
Comment:

Hi, could anyone please help me to correct this please?
I am doing Reverse Words In String.
example the input is: abc defg
the output will be: cba gfed

#include <stdio.h>
void main()
{
int a=0, c;
cha* st;
printf("enter string: ");
gets(st);
for(int i=0; st[i]!='\0'; i++)
{
if(st[i]==32)
{
printf(" ");
a++;
}
else
{
for(int b=a; st[b]!=32; b++)
c++;
if(i==c)
{
for(int x=c; st[x]!=a; x--)
printf("%c", st[x]);
}
}
}
return 0;
}


Report Offensive Message For Removal


Response Number 1
Name: Infinite Recursion
Date: November 11, 2003 at 09:17:09 Pacific
Reply: (edit)

Just something that caught my eye while glancing over your code...

cha* st;

Did you mean char* st; ?

IR


Report Offensive Follow Up For Removal

Response Number 2
Name: Ronin1
Date: November 11, 2003 at 12:22:26 Pacific
Reply: (edit)

Aren't you in need of memory for your pointer before you can use it?

You must declare variables at the start of a code block in C, so I don't believe that for(int i=0...) is valid.

Look into the strxxx family from string.h as those functions can simplify what you're looking to do.



Report Offensive Follow Up For Removal

Response Number 3
Name: Brian
Date: November 11, 2003 at 16:57:35 Pacific
Reply: (edit)

Let say I have fix it to char* st;. I'm sure for(int i=0; i!='\0'; i++) it work.
So anyone still can fix this, please? Thx.


Report Offensive Follow Up For Removal

Response Number 4
Name: Gagey
Date: November 12, 2003 at 22:59:03 Pacific
Reply: (edit)

declaring the int inside the loop is NOT standard C so if it compiles on your machine, then your compiler supports it as a non-standard feature, but other compilers will spit out an error at you!

variable decls MUST be the very first thing inside a block of code. You CAN declare then inside a loop or if (..) block, ie:

int i ;
for (i = 0 ; i < 10 ; i++) {
int j = .... ;

/* ....... */
}

And Ronin1 is right u need to allocate space for your string!

try:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INPUT 256
#define DELIMS " \n"

char *reverse(char *) ;

int main() {

char input[MAX_INPUT+1] ;
char *token ;

printf("enter string: ") ;
if ( fgets(input, MAX_INPUT, stdin) == NULL ) {
printf("Bad String!\n") ;
return 1 ;
}
else {
token = strtok(input, DELIMS) ;
while ( token != NULL ) {
printf("%s ", reverse(token)) ;
token = strtok(NULL, DELIMS) ;
}
printf("\n") ;
fgets(input, 100, stdin) ;
}

return 0 ;
}

char *reverse(char *str) {

char *end_ptr = str + strlen(str) - 1 ;
char *dup = strdup(str) ;
char *dup_ptr = dup ;

while ( end_ptr >= str )
*dup_ptr++ = *end_ptr-- ;

strcpy(str, dup) ;
free(dup) ; /* If u want to be xtra good! */
return str ;
}


Report Offensive Follow Up For Removal

Response Number 5
Name: Gagey
Date: November 12, 2003 at 23:03:57 Pacific
Reply: (edit)

MISTAKE:

The last fgets(....) just above the return 0 ;
in the main function is only there so the app dos window hung around till I hit Enter so I could check the output!


Report Offensive Follow Up For Removal


Response Number 6
Name: Iuri Cernov
Date: November 14, 2003 at 13:51:10 Pacific
Reply: (edit)

Using recursive you can use:

void print_reverse()
{
    char ch;
    ch = getche();
    if (ch != '\n') {
        print_reverse();
        putchar(ch);
    }
}

int main(void) { print_reverse(); }


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Please help me in C!

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge