Reputation: 779
Bolded is where I am trying to have the program ignore spaces in plain text when it is outputted. I'm confused on how to do this. When I run the program, it does not ignore the spaces. Instead, it is ran as if the bolded else if statement is not present. I'm confused about why this is. I'm sorry if my code is a bit messy. I've just started programming.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int shift(char c);
int main(int argc, string argv[])
{
// Check to see if two arguments are enterted at launch
int cipher = 0;
if (argc != 2)
{
// If not return error & return 0
printf("Usage: ./vigenere keyword \n");
return 1;
}
else
{
int strlength = strlen(argv[1]);
// Iterates through characters in second argument (key), checking to see
// if they are digits
for (int k = 0; k < strlength; k++)
{
if (!isalpha(argv[1][k]))
{
// If not return error & return 1
printf("Usage: ./vigenere keyword\n");
return 2;
}
}
//char *c =argv[1];
string plaintext = get_string("Plaintext: ");
int len = (int)strlen(plaintext);
//int b = atoi(c);
char code[len];
strcpy(code, plaintext);
for (int j = 0; j < len; j++)
{
int key = shift(argv[1][j]);
if (isupper(argv[1][0]))
{
cipher = ((((code[j] - 'A') + key) % 26) + 'A');
//printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
//printf("%c",cipher);
}
else if (islower(argv[1][0]))
{
cipher = ((((code[j] - 'a') + key) % 26) + 'a');
//printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
printf("%c",cipher);
}
else if (!isalpha(code[j]))
{
code[j] = 0;
}
/* else
{
printf("%c", code[j] + (cipher));
}
*/
}
printf("\n");
}
}
int shift(char c)
{
int i = c;
if (i <= 'Z' && i >= 'A')
{
return ((i - 'A') % 26);
}
else
{
return ((i - 'a') % 26);
}
}
Upvotes: 1
Views: 632
Reputation: 84561
While you never indicated whether the comments were sufficient to solve your problem, if you are still wrestling with how to take input with the CS50 get_string()
function while ensuring that it contains no whitespace, you can simply write a short wrapper for the get_string()
function.
In the wrapper function, you can simply pass any prompt directly to get_string()
saving the return. Then allocate a second string to hold the contents and loop over the string returned by get_string()
copying only the non-whitespace characters to the new block of memory, nul-terminating the new string when done and return the new string.
It could be something as simple as:
#include <cs50.h>
...
/* function using CS50 get_string() to fill input w/o spaces */
string get_str_nospc (string p)
{
size_t ndx = 0; /* index */
string s = get_string (p), /* string returned by get_string() */
sp = s, /* pointer to s (must preserve s address) */
s_nospc = NULL; /* pointer to copy with no whitespace */
if (!s) /* validate get_string() return */
return NULL;
/* allocate/validate storage for string without whitespace */
if (!(s_nospc = malloc (strlen (s) + 1))) {
perror ("malloc-s_nospc");
return NULL;
}
do /* copy s to s_nospc omitting whitespace */
if (!isspace (*sp)) /* if it's not a space - copy */
s_nospc[ndx++] = *sp;
while (*sp++); /* post-increment ensures nul-character copied */
return s_nospc; /* return string with no whitespace */
}
(note: you will want to allocate and copy regardless whether the string returned by get_string()
contains whitespace as the string returned by get_string()
is freed by the CS50 library destructor on exit. Your function should not return a string that may/may not need to be freed based on a condition within the function -- you would never know whether you were responsible for calling free
or not.)
A short example could be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cs50.h>
/* function using CS50 get_string() to fill input w/o spaces */
string get_str_nospc (string p)
{
size_t ndx = 0; /* index */
string s = get_string (p), /* string returned by get_string() */
sp = s, /* pointer to s (must preserve s address) */
s_nospc = NULL; /* pointer to copy with no whitespace */
if (!s) /* validate get_string() return */
return NULL;
/* allocate/validate storage for srting without whitespace */
if (!(s_nospc = malloc (strlen (s) + 1))) {
perror ("malloc-s_nospc");
return NULL;
}
do /* copy s to s_nospc omitting whitespace */
if (!isspace (*sp)) /* if it's not a space - copy */
s_nospc[ndx++] = *sp;
while (*sp++); /* post-increment ensures nul-character copied */
return s_nospc; /* return string with no whitespace */
}
int main (void) {
string nospc = get_str_nospc ("input : ");
if (nospc) {
printf ("nospc : %s\n", nospc);
free (nospc);
}
}
Example Use/Output
$ ./bin/cs50_getstrnospc
input : my dog has fleas
nospc : mydoghasfleas
Look things over and let me know if that is what you intended by your last comment. If not, I'm happy to help further.
Upvotes: 1