![]() |
|
|
#177 | |
|
Aug 2005
Seattle, WA
2×883 Posts |
Quote:
But let's face it, even the running time difference is negligible. He's doing I/O, which means the extra function call overhead involved in using getchar will be lost in the noise. Not to mention that while getchar may be a macro that expands to getc(stdin), getc itself is pretty much always a macro that doesn't result in a function call. |
|
|
|
|
|
|
#178 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
160658 Posts |
Well, even with the getchar() for loop, I still have to allocate the array to store the chars ahead of time, so I still have to guess.
|
|
|
|
|
|
#179 | |
|
Aug 2005
Seattle, WA
2×883 Posts |
Quote:
Doing so is a decent exercise. What strategy do you think makes sense for allocating space when there are more characters on the line than fit in your buffer? Just add a constant amount each time? Double the size? These are interesting things to consider, and give a good introduction to some issues that come up quite a lot in C. Of course, they also seem like a pain to have to consider if you're a novice to the language. But that's C for you. |
|
|
|
|
|
|
#180 | |
|
"Ben"
Feb 2007
7·503 Posts |
Quote:
To make it slightly more efficient, you could always realloc to the next higher power of 2, say, once the number of characters read exceeds the current allocation, and then to a final realloc to the final string size. I do storage via binary growth of arrays all the time for problems in which I don't know exactly how much data is going to be stored. |
|
|
|
|
|
|
#181 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
160658 Posts |
Yeah, I forgot about realloc. I put that aside so I can get the rest of the prog working first.
|
|
|
|
|
|
#182 | |
|
Aug 2005
Seattle, WA
2×883 Posts |
Quote:
To fix this you could of course just change the test to "if (i == 1)" or something like that. But more generally, you will usually not want that newline character in there when you want to do stuff with the line. So your best bet is to kill it. I.e. right after your call to strlen, you can put something like "if (string[i-1] == '\n') string[i-1] = '\0';" If you do that, I think you'll find things work as expected. Though of course you still have an egregious memory leak, and you still can't handle lines larger than the max size you chose. |
|
|
|
|
|
|
#183 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3·29·83 Posts |
That's among the reasons I initially implemented it myself, i.e. I knew exactly how it would behave. Thanks for the tip, I'll go try it now. (Although I don't think that will solve the problem of it not waiting for input the first time through the loop -- we'll see.)
Edit: Yep -- now it catches blanks correctly, but of course now the problem is that for whatever reason it doesn't wait to read the user's input the first time. So when I ran it I got Code:
Please enter the source text (empty line to quit) Bye. Last fiddled with by Dubslow on 2012-02-24 at 05:48 |
|
|
|
|
|
#184 | |
|
Aug 2005
Seattle, WA
176610 Posts |
Quote:
|
|
|
|
|
|
|
#185 |
|
Romulan Interpreter
Jun 2011
Thailand
3×3,221 Posts |
yeah, why sending/receiving thousands of network packages, when you could send/receive the whole terabyte in a single tcpip package... :P
|
|
|
|
|
|
#186 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
160658 Posts |
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRLEN 100
/* A program to take a string, and create its Ceasar Cypher using a shift value input by the user.
* (Entering 999 or -999 uses a letter's position as its shift value instead.)
*/
int isULetter(char c) { // boolean
if ('A' <= c && c <= 'Z')
return 1;
else
return 0;
}
int isLLetter(char c) { // boolean
if ('a' <= c && c <= 'z')
return 1;
else
return 0;
}
int main(void) {
int shift;
int badin; // boolean
int secret = 0; // boolean
int usecret = 0;// boolean
int i; // index
char c;
do { // get user input
printf("Please enter the shift value (between -25..-1 and 1..25)\n");
scanf("%d", &shift);
badin = !(((-25 <= shift) && (shift <= -1)) || ((1 <= shift) && (shift <= 25)));
if (shift == 999) {
secret = 1;
badin = 0;
} else if (shift == -999) {
usecret = 1;
badin = 0;
}
if (badin)
printf("%d is not a valid shift value.\n", shift);
} while (badin); // done getting input
if (secret || usecret)
printf("Using position shift\n");
else
printf("Using shift value of %d\n", shift);
if (shift < 0)
shift += 26;
while (1) { // the cypher stage
printf("Please enter the source text (empty line to quit)\n");
char * string = (char *)malloc( MAXSTRLEN * sizeof(char));
fgets(string, MAXSTRLEN, stdin);
i = (int)strlen(string);
if( i == (MAXSTRLEN - 1) ) { // Then the string is longer than MAXSTRLEN - 1, and so the rest is lost.
printf("Source is longer than %d characters. The rest is lost.\n", MAXSTRLEN-1); // will add in realloc later once the behavior is correct
}
if ( string[0]=='\n' ) { // The entire string is null, or rather, the only character was \n, so no input.
printf("Bye.\n");
return 7;
}
printf("Source : ");
puts(string); // put string; accepts pointer to char array as argument.
char * code = (char *)malloc( MAXSTRLEN * sizeof(char));
for ( i=0; ((c=string[i])!=0) && (i<(MAXSTRLEN-1)) ; i++) {
if ( isULetter(c) ) {
if (secret) {
c = (char) ('A' + ((c - 'A' + i) % 26));
} else if (usecret) {
c = (char) ('A' + ((c - 'A' + (26 - (i % 26))) % 26));
} else {
c = (char) ('A' + ((c - 'A' + shift) % 26));
}
} else if ( isLLetter(c) ) {
if (secret) {
c = (char) ('a' + ((c - 'a' + i) % 26));
} else if (usecret) {
c = (char) ('a' + ((c - 'a' + (26 - (i % 26))) % 26));
} else {
c = (char) ('a' + ((c - 'a' + shift) % 26));
}
}
code[i] = c;
}
printf("Processed: ");
puts(code);
} // while
} // main
Code:
bill@Gravemind:~/bin/c∰∂ cypher Please enter the shift value (between -25..-1 and 1..25) -999 Using position shift Please enter the source text (empty line to quit) Bye. bill@Gravemind:~/bin/c∰∂ For context, this is evolved from a Java assignment that was due on Monday. I'm trying to fulfill the specifications using C, so that my C doesn't fall behind Java. Last fiddled with by Dubslow on 2012-02-24 at 06:14 |
|
|
|
|
|
#187 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3×29×83 Posts |
I think I've got at least part of the problem. I disabled the scanf and hardcoded in a shift value; then the string prompt paused just fine for me without looping or exiting.
|
|
|
|