problems using demo arguments in c code do deal with the strings in argv - argv

I have a c code to show a lowercase of one argument:
This code runs ok when called from commandline with more than 6 argument
however,if called without argument, the demo part does not work ,and programme got stuck:
Below is the code file:
Can anyone help me, Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
int showLowerCase(char *argv[]){
char* aStr = argv[6];
//To lowercase: the demo got stuck probably here
for (int i = 0; aStr[i]; i++) {
aStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",aStr);
}
int main(int argc, char *argv[])
{
if(argc < 7){
// when no srguments given, try a demo:
// change argc to 7
argc = 7;
// make a example of argv2 with 7 strings
char *argv2[7];
argv2[0] = "killWindowsVersatile.exe";
argv2[1] = "key";
argv2[2] = "ci";
argv2[3] = "once";
argv2[4] = "2";
argv2[5] = "1000";
argv2[6] = "SuperCol"; //this argument will be shown as lowercase
showLowerCase(argv2);
return 0;
}
showLowerCase(argv);
return 0;
}
It seems that argv can be modified to lowercase in site, while the argv2 I constructed cannot be modified.

Solved by strcpy:
int showLowerCase(char *argv[]){
printf("aStr = argv[6]\n");
char* aStr = argv[6];
printf("aStr = argv[6]...done");
//make hard copy
char newStr[strlen(aStr)+1];
strcpy(newStr,aStr);
//To lowercase
for (int i = 0; newStr[i]; i++) {
printf("i=0\n");
printf("%c\n",newStr[i]);
newStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",newStr);
}

Related

How to return a int converted to char array back to main for displaying it

My doubts are as follows :
1 : how to send 'str' from function 'fun' , So that i can display it in main function.
2 : And is the return type correct in the code ?
2 : the current code is displaying some different output.
char * fun(int *arr)
{
char *str[5];
int i;
for(i=0;i<5;i++)
{
char c[sizeof(int)] ;
sprintf(c,"%d",arr[i]);
str[i] = malloc(sizeof(c));
strcpy(str[i],c);
}
return str;
}
int main()
{
int arr[] = {2,1,3,4,5},i;
char *str = fun(arr);
for(i=0;i<5;i++)
{
printf("%c",str[i]);
}
return 0;
}
how to send 'str' from function 'fun' , So that i can display it in main function.
This is the way:
char* str = malloc( size );
if( str == NULL ) {
fprintf( stderr,"Failed to malloc\n");
}
/* Do stuff with str, use str[index],
* remember to free it in main*/
free(str);
And is the return type correct in the code ?
No, Probably char** is the one you need to return.
the current code is displaying some different output.
Consider explaining what/why do you want to do ? The way you have written, seems completely messed up way to me. You're passing array of integer but not its length. How is the fun() supposed to know length of array? Another problem is array of pointers in fun().
You can't write a int to a char (See the both size). So I used char array instead.
However, I'm not sure if this is what you want to do (might be a quick and dirty way of doing it):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char**
fun(int *arr, int size)
{
char **str = malloc( sizeof(char*)*size );
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
int i;
for(i=0;i<5;i++) {
str[i] = malloc(sizeof(int));
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
sprintf(str[i],"%d",arr[i]);
}
return str;
}
int
main()
{
int arr[] = {2,1,3,4,5},i;
char **str = fun(arr, 5);
for(i=0;i<5;i++) {
printf("%s\n",str[i]);
free(str[i]);
}
free(str);
return 0;
}
I made these changes to your code to get it working:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **fun(int *arr)
{
char **str = malloc(sizeof(char *) * 5);
int i;
for(i = 0; i < 5; i++) {
if ((arr[i] >= 0) && (arr[i] <= 9)) {
char c[2] ;
sprintf(c, "%d", arr[i]);
str[i] = (char *) malloc(strlen(c) + 1);
strcpy(str[i],c);
}
}
return str;
}
int main()
{
int arr[] = {2, 1, 3, 4, 5}, i;
char **str = fun(arr);
for(i = 0; i < 5; i++) {
printf("%s", str[i]);
free(str[i]);
}
printf("\n");
free(str);
return 0;
}
Output
21345
I added a check to make sure that arr[i] is a single digit number. Also, returning a pointer to a stack variable will result in undefined behavior, so I changed the code to allocate an array of strings. I don't check the return value of the malloc calls, which means this program could crash due to a NULL pointer reference.
This solution differs from the others in that it attempts to answer your question based on the intended use.
how to send 'str' from function 'fun' , So that i can display it in main function.
First, you need to define a function that returns a pointer to array.
char (*fun(int arr[]))[]
Allocating variable length strings doesn't buy you anything. The longest string you'll need for 64bit unsigned int is 20 digits. All you need is to allocate an array of 5 elements of 2 characters long each. You may adjust the length to suit your need. This sample assumes 1 digit and 1 null character. Note the allocation is done only once. You may choose to use the length of 21 (20 digits and 1 null).
For readability on which values here are related to the number of digits including the terminator, I'll define a macro that you can modify to suit your needs.
#define NUM_OF_DIGITS 3
You can then use this macro in the whole code.
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
Finally the receiving variable in main() can be declared and assigned the returned array.
char (*str)[NUM_OF_DIGITS] = fun(arr);
Your complete code should look like this:
Code
char (*fun(int arr[]))[]
{
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
int i;
for(i=0;i<5;i++)
{
snprintf(str[i],NUM_OF_DIGITS,"%d",arr[i]); //control and limit to single digit + null
}
return str;
}
int main()
{
int arr[] = {24,1,33,4,5},i;
char (*str)[NUM_OF_DIGITS] = fun(arr);
for(i=0;i<5;i++)
{
printf("%s",str[i]);
}
free(str);
return 0;
}
Output
2413345
With this method you only need to free the allocated memory once.

Vigenere.c CS50 Floating Point Exception (Core Dumped)

I am working on the Vigenere exercise from Harvard's CS50 (in case you noticed I'm using string and not str).
My program gives me a Floating Point Exception error when I use "a" in the keyword.
It actually gives me that error
when I use "a" by itself, and
when I use "a" within a bigger word it just gives me wrong output.
For any other kind of keyword, the program works perfectly fine.
I've run a million tests. Why is it doing this? I can't see where I'm dividing or % by 0. The length of the keyword is always at least 1. It is probably going to be some super simple mistake, but I've been at this for about 10 hours and I can barely remember my name.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main (int argc, string argv[])
{
//Error message if argc is not 2 and argv[1] is not alphabetical
if (argc != 2)
{
printf("Insert './vigenere' followed by an all alphabetical key\n");
return 1;
}
else if (argv[1])
{
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isalpha((argv[1])[i]) == false)
{
printf("Insert './vigenere' followed by an all alphabetical key\n");
return 1;
}
}
//Store keyword in variable
string keyword = argv[1];
//Convert all capital chars in keyword to lowercase values, then converts them to alphabetical corresponding number
for (int i = 0, n = strlen(keyword); i < n; i++)
{
if (isupper(keyword[i])) {
keyword[i] += 32;
}
keyword[i] -= 97;
}
//Ask for users message
string message = GetString();
int counter = 0;
int keywordLength = strlen(keyword);
//Iterate through each of the message's chars
for (int i = 0, n = strlen(message); i < n; i++)
{
//Check if ith char is a letter
if (isalpha(message[i])) {
int index = counter % keywordLength;
if (isupper(message[i])) {
char letter = (((message[i] - 65) + (keyword[index])) % 26) + 65;
printf("%c", letter);
counter++;
} else if (islower(message[i])) {
char letter = (((message[i] - 97) + (keyword[index])) % 26) + 97;
printf("%c", letter);
counter++;
}
} else {
//Prints non alphabetic characters
printf("%c", message[i]);
}
}
printf("\n");
return 0;
}
}
This behavior is caused by the line keyword[i] -= 97;, there you make every 'a' in the key stream a zero. Later you use strlen() on the transformed key. So when the key starts with an 'a', keywordLength therefor is set to zero, and the modulo keywordLength operation get into a division by zero. You can fix this by calculating the keyword length before the key transformation.

c - too many values for a hex number

Could somebody please clarify what's happening here that's giving me a hex value of 0xffffffa5 versus 0xa5?
#define HEXVAL 0xA5
int main(int argc, char *argv[])
{
int numBytes = 3;
char *tmp;
char *pyld;
pyld = malloc(numBytes * sizeof(char));
memset(pyld, 0, sizeof(pyld));
tmp = pyld;
*(tmp) = HEXVAL;
printf("out = %x\n", pyld[0]); // -> ffffffa5
}
I was expecting "out = a5" instead of all "out = ffffffa5". I would like to add signed values to pyld so I'd rather not set pyld to unsigned char.
Thanks!
To begin, may I suggest using calloc? It formats the heap space for you.
To fix your problem, you have to make tmp unsigned. You can keep pyld signed. Just remember to deference tmp to print out the right value.
Here's my work:
#include <stdio.h>
#include <stdlib.h>
#define HEXVAL 0xA5
int main(int argc, char *argv[])
{
int numBytes = 3;
unsigned char *tmp;
char *pyld;
pyld = calloc(numBytes, sizeof(char));
tmp = pyld;
*tmp = HEXVAL;
printf("pyld = %X\n", *pyld);
printf("tmp = %X\n", *tmp);
return 0;
}
Here's the output
pyld = FFFFFFA5
tmp = A5
Hope that helps!

Setting number of columns in output window with c++

I have created a Fibonacci program that runs correctly. However I can not figure out how to format the output window the way the problem would like. The rows and spacing are correct but the program should display 6 columns, as it is now the program outputs nine with the ninth cut off. Am I doing something wrong or missing something? I am using the Visual Studio C++ compiler.
#include <iostream>
#include <iomanip>
using namespace std;
void main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << FirstNum << setw (10);
Count++;
} while (Count < 40);
}
This code will generated only 6 columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << setw (10)<< FirstNum ;
Count++;
if(Count%6==0)
cout<<endl;
} while (Count < 40);
return 0;
}

Weird define in C++ preprocessor

I've come across this
#define DsHook(a,b,c) if (!c##_) { INT_PTR* p=b+*(INT_PTR**)a; VirtualProtect(&c##_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c##_=*p; VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no); *p=(INT_PTR)c; }
and everything is clear except the "c##_" word, what does that mean?
It means to "glue" together, so c and _ get "glued together" to form c_. This glueing happens after argument replacement in the macro. See my example:
#define glue(a,b) a##_##b
const char *hello_world = "Hello, World!";
int main(int arg, char *argv[]) {
printf("%s\n", glue(hello,world)); // prints Hello, World!
return 0;
}
It is called a token-pasting operator. Example:
// preprocessor_token_pasting.cpp
#include <stdio.h>
#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
int main()
{
paster(9);
}
Output
token9 = 9
That's concatenation that appends an underscore to the name passed as c. So when you use
DsHook(a,b,Something)
that part turns into
if (!Something_)
After the preprocessor, your macro will be expanded as:
if (!c_) { INT_PTR* p=b+*(INT_PTR**)a; VirtualProtect(&c_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c_=*p; VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no); *p=(INT_PTR)c; }
The ## directive concatenates the value of c which you pass as a macro parameter to _
Simple one:
#define Check(a) if(c##x == 0) { }
At call site:
int varx; // Note the x
Check(var);
Would expand as:
if(varx == 0) { }
It is called Token Concatenation and it is used to concatenate tokens during the preprocessing
For example the following code will print out the values of the values of c, c_, c_spam:
#include<stdio.h>
#define DsHook(a,b,c) if (!c##_) \
{printf("c=%d c_ = %d and c_spam = %d\n",\
c, c##_,c##_spam);}
int main(){
int a,b,c=3;
int c_ = 0, c_spam = 4;
DsHook(a,b,c);
return 0;
}
Output:
c=3 c_ = 0 and c_spam = 4

Resources