Reflecting while studying C language
Run the following C program
#include <stdio.h>
#include <string.h>
int main (int argc, char const *argv[]){
if(!argv[1]) return 0;
int i = strlen(argv[1]);
while(i > -1){
printf("%c",argv[1][i]);
i–;
}
printf("\n");
}
#include <string.h>
int main (int argc, char const *argv[]){
if(!argv[1]) return 0;
int i = strlen(argv[1]);
while(i > -1){
printf("%c",argv[1][i]);
i–;
}
printf("\n");
}
Using this string as first program parameter:
"..eromyna ti ekat t'nac I ?nwod edispu semit ynam os dlrow eht si yhW"
Hey.. I tried to run the program with the parameter “LOL”, but it doesn’t seem to work to me… =)
*LOL*
Hhhhmmm have you tried turning your pc off and on again?
* ROFL *
Loool!! and now with pointers
#include
#include
int main (int argc, char** argv)
{
if (argc != 2)
return 0;
char* c = argv[1] + strlen(argv[1]);
while (c– != argv[1])
printf(“%c”, *c);
printf(“\n”);
}
“c–” should be “c minus minus”.. damn autoformatting!
“Why is the world so upside down? I can’t take it anymore..”
@ami
suineg a era uoy,aitnama
@benjamin
Yes, you were right
Pointers are so cool! I just wrote this code today morning, without looking at your
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[]){
if(!argv[1]) return 0;
char *p = argv[1] + strlen(argv[1]);
while(p--!=argv[1]){
printf("%c",*p);
};
printf("\n");
}
#include <stdio.h>
main (int argc, char const *argv[]){
char *p;
p = &argv[1][0];
int a = strlen(argv[1]);
while (a >= 0){
printf("%c", *(p+a));
a--;
}
printf("\n");
}
Less geek, but still working