Modify contents of string literal C++ based on function constraints - c++

I need to convert a given string literal to its lower case by passing the string as an argument to the function Lower(char *). The function can return only int.
In the main() i need the converted string to be accessible somehow.
Currently i am type casting the address of the tmp string in the Lower() function and then trying to dereference that address.
This is the code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int Lower(char* str)
{
char tmp[100];
for(int i=0;i<=strlen(str);i++)
{
if(isupper(*(str+i)))
*(tmp+i) = *(str+i)+32;
else
*(tmp+i) = *(str+i);
}
return int(tmp);
}
int main()
{
char* str = "gOAt";
char *rc=(char *)Lower(str);
printf(rc);
return 0;
}
Again the function prototype cannot change ie the prototype must be int Lower(char *). However, "goat" must be somehow accessible in the main() function. How can this be done?

There are multiple things wrong with the posted code. My take would be that the int returned from the function is a status code, when it's 1, it signals that the conversion to lowercase was successful, 0 otherwise. Here is a working program that does the conversion:
#include <iostream>
using namespace std;
int lower(char* str)
{
int hasLower = 0;
while(*str)
{
if (*str >= 'A' && *str <= 'Z')
{
*str = (*str - 'A') + 'a';
hasLower = 1;
}
str++;
}
return hasLower;
}
int main(int argc, char** argv)
{
if (argc != 2) { cout << "No input! Exiting..." << endl; return 1; }
char* input = argv[1];
if (lower(input))
{
cout << "Lower case: " << input << endl;
}
else
{
cout << input << " isn't a valid string for lower-case conversion" << endl;
}
return 0;
}

Related

C++ add checks to avoid reading two arguments

My code currently reads both arguments and i need to add a check for it to read 1 argument if someone put in one number such as 100 and to read the second argument if entered 100 3.
right now it reads both arguements everytime and and gives an error if one argument is entered.
#include <iostream>
#include <cstring>
using namespace std;
int perfectnumber(int number)
{
int sumofdivisor = 0;
for (int i = 1; i < number; i++)
{
if (number % i == 0)
sumofdivisor += i;
}
return abs(sumofdivisor - number);
}
int main(int argc, char *argv[])
{
int count = atoi(argv[2]);
int upper_limit = atoi(argv[1]);
for (int start = 2; start <= upper_limit; start++)
{
int difference = perfectnumber(start);
if (difference <= count)
{
cout << start << " ";
}
}
cout << endl;
}
The parameter argc is your friend, it tells you how many arguments there are.
Here is an example of how to use argc.
#include "stdio.h"
int main(int argc, char* argv[])
{
printf("Number: %d\n", argc);
printf("0: %s\n", argv[0]);
if (1<argc)
{
printf("1: %s\n", argv[1]);
}
}
You can use argc to see how many arguments are provided. The first argument is the name of the executable1, so you have to compare with 3. Here is a simple example:
#include <iostream>
#include <cstdlib>
int main(int argc, char *argv[])
{
if (argc < 3) {
std::cerr << "Too few arguments\n";
return EXIT_FAILURE;
}
std::cout << "Args: " << argv[1] << " and " << argv[2] << '\n';
}
1 This is not entirely correct. According to this reference: "argv[0] is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment)." But as a comment points out, this is not entirely accurate either. This is a convention that implementations usually follow but are free to not to.

Lowercasing Capital Letters in char array[] in C++ through Pointers

I am trying to use pointers to recursively lowercase all capital letters
using the C++ programming language. Below is the code snippet:
// Example program
#include <iostream>
#include <string>
using namespace std;
void all_lower(char* input) {
if ( *input ) {
cout << input << endl;
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
cout << *input << endl;
all_lower(++input); // simply move to next char in array
}
int main() {
char test[] = "Test";
all_lower(test);
return 0;
}
The output ends up being:
"Test"
even though I tried to increase the ASCII code value of the element by 32.
You are exiting the function on the first non-null character detected, which is 'T', and then you output the entire array before exiting, so you are seeing the original unmodified input. You are not recursing through the array at all. You need to recurse through the array until you reach the null terminator.
You need to change this:
if ( *input ) {
cout << input << endl;
return;
}
To this instead:
if ( *input == 0 ) {
return;
}
Then the function will work as expected.
That being said, I suggest you remove the cout statements from the function, and do a single cout in main() after the function has exited. This will speed up the function, and prove that the content of the test[] array is actually being modified:
#include <iostream>
using namespace std;
void all_lower(char* input)
{
if ( *input == 0 ) {
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
all_lower(++input); // simply move to next char in array
}
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
all_lower(test);
cout << "After: " << test << endl;
return 0;
}
Live Demo
And, since you are using C++, consider removing all_lower() altogether and use the STL std::transform() algorithm instead:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
transform(test, test+4, test, [](char ch){ return tolower(ch); });
cout << "After: " << test << endl;
return 0;
}
Live Demo
Something short and easy:
#include <iostream>
#include <string>
using namespace std;
void all_lower(const char* input) {
if (!*input) {
std::cout << std::endl;
return;
}
std::cout << (char)(std::isalpha(*input) ? tolower(*input) : *input);
all_lower(++input); // simply move to next char in array
}
int main() {
all_lower("Test");
return 0;
}

Taking parameters on the command line in C++

I've a program that takes two csv files and a checkin date as inputs and renders a certain output. Thus I'd normally run the executable in this manner,
./my_executable file.csv 2015-10-13
However my requirement is to have the usage behave this way
my_executable --input1 ./file1.csv --input2 ./file2.csv --date 2015-08-01
How can I do this. Do I have write the words input1, input2 and date somewhere in my code. Any help appreciated.
Simplest way I can think of:
Live On Coliru
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <cassert>
int main(int argc, char *raw_argv[]) {
using namespace std;
vector<string> const args { raw_argv+1, raw_argv+argc };
assert(args.size() < 1 || args[0] == "--input1");
assert(args.size() < 3 || args[2] == "--input2");
if (args.size() > 4) {
std::string const& csv1 = args[1];
std::string const& csv2 = args[3];
std::string date = args.size() > 4? args[4] : "(unspecified)";
std::cout << "Arguments received: " << csv1 << ", " << csv2 << " date:" << date << "\n";
}
}
Prints e.g.
./test --input1 stuff.csv --input2 other.csv
Arguments received: stuff.csv, other.csv date:(unspecified)
Command line arguments are passed to your program via the argument count and argument list parameters of main:
int main(int argument_count, char * argument_list[]);
The first parameter is the number of arguments, including the name of your executable.
The second argument is an array of C-style strings, one for each argument (or word) on the command line. The first item is usually the name of the program.
You can always write a small program to test this out:
#include <iostream>
int main(int arg_count, char * arg_list[])
{
for (unsigned int i = 0; i < arg_count; ++arg_count)
{
std::cout << "Argument " << i << ": " << arg_list[i] << std::endl;
}
return EXIT_SUCCESS;
}
Edit 1:
Your parameters would line up as:
Argument 0: my_executable
Argument 1: --input1
Argument 2: ./file1.csv
Argument 3: --input2
Argument 4: ./file2.csv
//...
If you want to compare these parameters, then yes, you would need to type "input1":
//...
std::string arg1 = arg_list[1];
if (arg1 == "--arg1")
{
//...
}
This should give you a kickstart.
https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3
or if you want to handle the arguments manually.
see: https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments
int main(int argc, const char **argv[])
{
for(int i = 0; i < argc; i++) {
std::cout << argv[i] << std::endl;
}
return 0;
}
Usually when you give argument in that way the order should not matter, so you'll have to be able to parse the arguments in any order.
Here is a possible solution:
struct arguments
{
std::string input1;
std::string input2;
std::string date;
};
bool parse_arguments(int argc, char** argv, arguments& args)
{
if(argc < 7){ //or set defaults
//print usage();//implement
return false;
}
for(int i=1; i<argc;i+=2){
string command = argv[i];
string argument = argv[i+1];
if(command == "--input1"){
args.input1 = argument;
}
else if(command == "--input2"){
args.input2 = argument;
}
else if(command == "--date"){
args.date = argument;
}
else{
std::cerr<<"Unknown argument: " <<command<<std::endl;
//print usage();
return false;
}
}
if(args.input1.empty() || args.input2.empty() || args.data.empty())
return false;
return true;
}
int main(int argc, char* argv[]){
arguments args;
parse_arguments(argc,argv, args);
//now you can use the struct.
...
}

Why do I get an Segmentation fault error

My code works fine on codeblocks compiler on my computer but when I upload it to an online editor I get an Segmentation fault error and I don't know why.
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
ifstream stream(argv[1]);
char line[1000];
int x,last=-1;
while (stream>>line)
{
x = atoi(strtok(line,","));
cout<<x;
last=x;
while(x=atoi(strtok(NULL,",")))
{
if(x!=last)
{
cout<<","<<x;
last=x;
}
}
cout<<endl;
}
return 0;
}
You are given a sorted list of numbers with duplicates. Print out the sorted list with duplicates removed.
And this is the input
6,7,8,9,9,10,11,12,13,14,15
11,12,13,14,15,16,17,18,19,20
2,2,2,2,2
10,11,12,13,14,15,16,16,17
13,14,14,15,16,17,17,17,18
15,16,17,17,18,18,18,18,19,19,20
2,3,4,5,5
13,14,15,16,17
10,11,12,13,14,15,15,15,15,16,16,16
12,13,14,15,16,17,17,18
5,6,7,8,9,10,11
14,14,14,15,15,16,17,17,18,19,19,20,21,22
13,14,15,16,16,17,17,18
15,16,17,18,19,20,21,21,21,21,22,22
6,6,6,7,8,9,10,11,11,11,12,12,13
12,12,13,14,15,15,16,17,17,18,19,19,20,21
8,9,9,9,10,10,11,12,13,13,14,15
12,13,14,15,16,17,18
1,1,1,2,2,3,3,4,4
1,2,3,4
Since you're asking us to guess, let's start at the top ....
The code doesn't check that argv[1] is valid. If not, then you just dereferenced a null-pointer, and that caused your segmentation fault.
Does your "online editor" pass parameters? I suggest checking argc > 1.
Next, your code looks like it will pass a null pointer to atoi at the end of every line. That's another segmentation fault.
You are calling atoi with the result of strtok.
If strtok doesn't find anything it returns a null pointer.
This is the case at the end of the line.
So you are passing a null pointer to atoi which then leads to a crash.
Using your example this should work:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream stream(argv[1]);
char line[1000];
char* ln;
char* num;
int x;
int last;
while (stream >> line)
{
ln = line;
last = -1;
while (num = strtok(ln, ","))
{
x = atoi(num);
if (x != last)
{
if(last != -1) cout << "," << x;
else cout << x;
last = x;
}
ln = NULL;
}
cout << endl;
}
return 0;
}
EDIT: Another solution with checking for valid paramters and w/o strtok and atoi:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " <file>";
return 1;
}
ifstream stream(argv[1]);
if (!stream.is_open())
{
cout << "Failed to open file \"" << argv[1] << "\"";
return 2;
}
char line[1000];
while (stream >> line)
{
int last = -1;
int x = 0;
for (char* pos = line; pos < line + strlen(line); pos++)
{
if (*pos >= '0' && *pos <= '9')
{
x = (x * 10) + (*pos - '0');
}
else
{
if (last != x)
{
if (last != -1) {
cout << ',';
}
cout << x;
last = x;
}
x = 0;
}
}
cout << endl;
}
return 0;
}

Checking to make sure argv[1] is an integer c++

For my program I have to make sure the user only inputs a positive INTEGER. for example if the user inputted 12hi it should not run the program and print to std error. I am not quite sure how to implement this.
int main(int argc, char *argv[])
{
if(atoi(argv[1]) < 1)
{
cerr << "ERROR!"<< endl;
return 1;
}
return 0;
}
Pass it to a std::istringstream and ensure all data was processed:
if (a_argc > 1)
{
std::istringstream in(a_argv[1]);
int i;
if (in >> i && in.eof())
{
std::cout << "Valid integer\n";
}
}
See online demo at http://ideone.com/8bEYJq.
Ok, my revised answer. sscanf wasn't behaving how I thought it would and strtol provides the best C-like solution that is very portable.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
for (int i=1; i < argc; i++){
char* end;
long val = strtol(argv[i], &end, 10);
if (argc >= 2 && !end[0] && val >= 0){
printf("%s is valid\n", argv[i]);
} else {
printf("%s is invalid\n", argv[i]);
}
}
return 0;
}
Sample output:
./a.out 10 -1 32 1000 f -12347 +4 --10 10rubbish
10 is valid
-1 is valid
32 is valid
1000 is valid
f is invalid
-12347 is valid
+4 is invalid
--10 is invalid
10rubbish is invalid
This works because strtol will convert the argument to a long int. Then if end[0] is not at the end of the string it will be non-zero meaning it'll throw up an error for 10rubbish but be ok for values like 10. Then of course we only want positive integers and I've included the value 0 in that set.
atoi() by itself is not good enough as it returns zero for failure. 0 could be a valid input.
sscanf() also by itself is not good enough because it'll successfully convert strings like 10rubbish and return the value 10.
I realise op only wants argv[1], this answer scans through all provided args just to show the output of lots of valid and invalid entries.
Since you evidently do not object to using the Standard C library,
the function
long strtol (const char* str, char** endptr, int base)
from <cstdlib> is quite sufficient to ensure that the
commandline argument is a (long) integer numeral with an optional
"-" or "+" prefix, and nothing more than that. You merely need to
check that the char * stored at endptr on return addresses '\0',
which tells you that the function has consumed the entire argument.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
return 1;
}
char * endp;
long i = strtol(argv[1],&endp,10);
if (!*endp) {
cout << "The value of \"" << argv[1] << "\" is " << i << endl;
return 0;
}
cerr << "\"" << argv[1] << "\" is not an integer" << endl;
return 1;
}
LATER ...or catering for Steve Jessop's comments:
#include <cstdlib>
#include <iostream>
#include <climits>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
return 1;
}
char * endp;
long i = strtol(argv[1],&endp,10);
if (*endp) {
cerr << "\"" << argv[1] << "\" is not an integer :(" << endl;
return 1;
}
if (endp == argv[1]) {
cerr << "Empty string passed :(" << endl;
return 1;
}
if (i < 0) {
cerr << "Negative " << i << " passed :(" << endl;
return 1;
}
if (i <= INT_MAX) {
cout << "Non-negative int " << i << " passed :)" << endl;
} else {
cout << "Non-negative long " << i << " passed :)" << endl;
}
return 0;
}
A wrapper function would be in order for this degree of discrimination. And there
remains the very-very corner case that an input of ULONG_MAX will be accepted
as LONG_MAX.
You can try checking if all the characters in argv[1] are digits (possibly with a leading minus sign). The check can be performed by using the standard library function isdigit().
http://www.cplusplus.com/reference/cctype/isdigit/
Complete solution based on OP's actual code (also available at http://codepad.org/SUzcfZYp):
#include <stdio.h> // printf()
#include <stdlib.h> // atoi()
#include <ctype.h> // isdigit()
int main(int argc, char *argv[])
{
if( argc != 2 ) {
return 0;
}
char * pWord = argv[ 1 ];
char c = 0;
for( int i = 0; c = pWord[ i ], c ; ++i ) {
if( ! isdigit( c ) ) {
return 0;
}
}
int argvNum = atoi( argv[ 1 ] );
printf( "argc = %d, argv[ 1 ] = %s, argvNum = %d\n",
argc, argv[ 1 ], argvNum );
}
I'm new to C++ so please don't flame me if this is wrong, but couldn't you throw an exception and allow the user to re-correct the input?
I've learned a few ways of dealing with errors:
If/Else handling
Assert
Throw exception
1.IF/ELSE
#include
int main(int argc, int **argv) {
if (!isdigit(argv[1])) {
// handle code if it's not a digit.
return 0;
}
}
This is probably the easiest way to make sure
2.ASSERT
#include
int main(int argc, int *argv[]) {
assert(isdigit(argv[1]));
}
* Assert will terminate the program if argv[1] is not a digit
3.THROW
#include
using namespace std;
class Except {};
int main(int argc, int **argv) {
try {
isdigit(argv[1]);
throw Except();
// this code will not be executed
// if argv[1] is not a digit
}
catch (Except) {
cout << "argv[1] is not a digit.";
// handle exception or rethrow
}
}
It is definitely worth noting that throwing an exception will create a stack trace and also all code in-between the thrown exception and the block that catches the exception will NOT be executed.