The queueshift N utility reads a stream of characters one at a time. If they\'re
ID: 670441 • Letter: T
Question
The queueshift N utility reads a stream of characters one
at a time. If they're alphabetic, they are rotated forward
N places in the alphabet, and rotated around to the beginning
if they fall off the end of the alphabet.
You must read the COMMAND LINE* for the "shift" value. It will be in
argv[1]...
Example: a.out 13
(the "13" is passed into the executing program as the CString ['1''3''']
...which is an array of chars. Convert the array of chars to an
int with int atoi(char *) ; (On Buffy, you can run: man atoi).
So "hello" becomes "uryyb'. And "uryyb' becomes "hello" when it's
queueshift 13'd.
When running, the program should keep track of whether the input char
was lower or upper case. If input is not alphabetic, don't change it,
just pass it through. Don't delete anything from the input stream such
as linefeeds, or punctuation.
Start with a waterpump:
#include <iostream>
using namespace std ;
int main()
{
int c ;
int shift = atoi(argv[1]) ; // get the command-line parm
c = cin.get() ;
while ( ! cin.eof() )
{
cout.put(c) ;
c = cin.get() ;
}
}
Consider:
isalpha()
isupper()
"a char is just a small int"
+=
%=
Explanation / Answer
Instead of using atoi function. I would suggest sscanf:
char myarray[5] = {'-', '1', '2', '3', ''};
int i;
sscanf(myarray, "%d", &i);
It's very standard, it's in the stdio.h library .
And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string,
and probably also allows for non-number characters at the end.
A short example of its use:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int char2int (char *array, size_t n);
int main (void) {
char myarray[4] = {'-','1','2','3'};
char *string = "some-goofy-string-with-123-inside";
char *fname = "file-0123.txt";
size_t mlen = sizeof myarray/sizeof *myarray;
size_t slen = strlen (string);
size_t flen = strlen (fname);
printf (" myarray[4] = {'-','1','2','3'}; ");
printf (" char2int (myarray, mlen): %d ", char2int (myarray, mlen));
printf (" string = "some-goofy-string-with-123-inside"; ");
printf (" char2int (string, slen) : %d ", char2int (string, slen));
printf (" fname = "file-0123.txt"; ");
printf (" char2int (fname, flen) : %d ", char2int (fname, flen));
return 0;
}
Note: when faced with '-' delimited file indexes (or the like), it is up to you to negate the result.
(e.g. file-0123.txt compared to file_0123.txt where the first would return -123 while the second 123).
Example Output
$ ./bin/atoic_array
myarray[4] = {'-','1','2','3'};
char2int (myarray, mlen): -123
string = "some-goofy-string-with-123-inside";
char2int (string, slen) : -123
fname = "file-0123.txt";
char2int (fname, flen) : -123
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.