Need help on C

0 Yuaan Rai · March 15, 2015
i am new to programming. Today i was learning c where if we typed a letter it would get reverse typed. My friends were using devc++ and i am using codeblocks . Our teacher wrote us a code and when i copied and tried to run it ran but the words didnt get reversed however it worked nicely in all of my friends laptop. Here is the source code plss i would be greatful if anyone would point out the mistake.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
    char source[100],result[100];
    int total, last, start;
    printf("input a string\n");
    gets(source);
    puts(source);
    total=strlen(source);
    for (last=total-1, start=0; last>=0; last--, start++);
    result[start]=source[last];
    result[last]='\0';
    puts(result);
    getch();
    return 0;
}  

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 20, 2015
I was trying to find somewhere that I could compile C code easily because it wasn't quite working correctly and I didn't want to make a brand new MakeFile every single time.  Anyway, I finished setting up Eclipse to do basic C compilations from now on. 
0 Yuaan Rai · March 16, 2015
hey Arjun i tried running your code but it doesnot reverse the word i type. It gives out the same word.
This is the code i copied from the internet and it works perfectly.
#include<stdio.h>
#define SIZE 50// Maximum length of sentence
int main()
    {
        int i,j,pivot;
        char s[SIZE],temp[SIZE/2];
        printf("\nInput string: ");
        gets(s); // scanf won't read spaced words
        for(i=0,pivot=0;s!='\0';i++) // loops till null character encountered
            {
                if(s==32||s[i+1]=='\0') // 32 is ASCII value of space
                   {
                       for(j=i;j>=pivot;j--)
                       printf("%c",s[j]);
                       pivot=i+1;printf("%c",32);
                   }
            }
        return 0;
    }
anyway thanks Arjun
0 c student · March 16, 2015

... for safety ...


gets (source);

Yeah, no.  gets(), more like getrekt().
0 Dol Lod · March 16, 2015
You're right. I should have tested it first. I try to keep the code as close to the original as possible so it is easier to figure out what is missing or what is wrong with it.  Then I try commenting on the portions I introduce to explain why I made the changes I did.  Still, I will think more about whether it is more beneficial to simply provide more general feedback.I normally do test my C++ code on visual studio. This is exactly why am I trying to write code out for practice. Still, I will refrain from giving out untested code unless it is untested.
0 Yuaan Rai · March 22, 2015
thanks guys i dont know why my replies are not even being published here. especial thanks to the person who has a name of a website. you sir are a great teacher
0 Dol Lod · March 15, 2015
I would make a few changes to this code for safety and simplicity. Try it now. 


#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char source[100],result[100];
int total, last, start;
for(start=0,last=100;start<last;start++) //fills everything with null bytes to avoid the possibility of lingering data.
{
source[start]='\0';
result[start]='\0';
}
printf("input a string\n");
gets(source);
if(total>98) //adjust for overflow
{
total=99;
source[99]='\0';
}
puts(source);
total=strlen(source);
for (last=total-1, start=0;start<total; last--, start++)
result[start]=source[last];
puts(result);
getch();
return 0;
}
  • 1

C

107,203 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator