What I want to do is write arguments in the command line separated by a + and concatenate the arguments into a single string
eg:
./concat Wow + this + is + cool
Wow this is cool
I looked up a question for this sort of topic before but that involved concatenating only the first character of each argument and not the entire arguments. And it didn't ignore the separator
This is what I have
void concat(char **argv, int argc, char *string)
{
size_t i = 0;
for(int j=1; j<argc; j++)
{
string[i++] = *argv[j];
if(j+1 != argc)
{
string[i++] = ',';
string[i++] = ' ';
}
}
string[i] = '\0';
}
And this is what I'm doing in main to call this function
int main(int argc, char *argv[])
{
int allnum=0;
char string[1000];
concat(argv, argc, string);
printf("%s\n", string);
}
Using the strcpy or strcat in string.h is more simple to concatenate string.
For example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char * s = malloc (2 * argc *sizeof (char));
if (argc < 2)
exit(-1);
for(int i = 1; i < argc; i += 2) {
strcat(s, argv[i]);
strcat(s, " ");
}
printf("%s\n", s);
return 0;
}
Related
In C programming language, is it possible to access int argc or char **argv without using the parameters? I know some of you might ask why this is needed, just for research purposes.
Is it possible to generate the cmd line arguments without using the main parameter variables ? For example, to illustrate some pseudo code, that i have in mind,
LPTSTR cmd = GetCommandLine();
splitted = cmd.split(" ") //split from spaces
char **someArgv.pushForEach Splitted, length++
and you'd have a someArgv with the parameters and length as argc, this'd really help to know if possible to illustrate.
If OP already has the command as a string, then:
Form a copy of the string
Parse it for argument count
Allocate for argv[]
Parse & tokenize copy for each argv[]
Call main()
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// Not standard, but commonly available
char *strdup(const char *s);
// Return length of token and adjust offset to the next one
// Adjust as needed
// Presently only ' ' are used to separate
// More advanced would have escape characters, other white-space, etc.
size_t tokenize(const char *s, size_t *offset) {
// find following space
size_t len = strcspn(s + *offset, " ");
*offset += len;
// find first non-space
*offset += strspn(s + *offset, " ");
return len;
}
int call_main(const char *cmd) {
char *cmd2 = strdup(cmd);
cmd2 += strspn(cmd2, " "); // skip leading spaces
size_t offset = 0;
int argc = 0;
while (tokenize(cmd2, &offset) > 0) {
argc++;
}
char **argv = malloc(sizeof *argv * ((unsigned)argc + 1u));
offset = 0;
for (int a = 0; a < argc; a++) {
argv[a] = &cmd2[offset];
size_t len = tokenize(cmd2, &offset);
argv[a][len] = '\0';
}
argv[argc] = NULL;
int retval = 0;
#if 0
retval = main(argc, argv);
#else
printf("argc:%d argv:", argc);
for (int a = 0; a < argc; a++) {
printf("%p \"%s\", ", argv[a], argv[a]);
}
printf("%p\n", argv[argc]);
#endif
free(cmd2);
free(argv);
return retval;
}
Sample
int main() {
call_main(" name 123 abc 456 ");
}
argc:4 argv:0x800062322 "name", 0x800062327 "123", 0x80006232c "abc", 0x800062331 "456", 0x0
Pedantic: The strings provided to main() should be modifiable. Avoid code like
argv[1] = "Hello";
....
main(argc, argv);
#include <stdio.h>
int main(int argc, char *argv[]);
int callMain(void)
{
char *argv[4];
argv[0] = "binary";
argv[1] = "param1";
argv[2] = "param2";
argv[3] = NULL;
return main(3, argv);
}
int main(int argc, char *argv[])
{
if (argc <= 1)
{
return callMain();
}
printf("ARGC: %u\n", argc);
int i;
for (i = 0; i < argc; i++)
printf("ARG: %u - %s\n", i, argv[i]);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *lineptr[] = {"abc","defg","hijk"};
char **lp = lineptr;
int nlines = 3;
while(nlines-- > 0){
printf("%s\n", *lineptr++);
}
while(--argc)
printf("%s%s",*++argv,(argc>1) ? " " : "");
printf("\n");
return 0;
}
You had a bug at the first printf statement:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *lineptr[] = {"abc","defg","hijk"};
char **lp = lineptr;
int nlines = 3;
while(nlines-- > 0){
printf("%s\n", lineptr[nlines]); /* see the difference to your line */
}
while(--argc)
printf("%s%s",*++argv,(argc>1) ? " " : "");
printf("\n");
return 0;
}
By the way, it's better to use a FOR-loop when handling arrays, because it's easier to manage and less prone to errors.
for(int i=0; i<nlines; i++) /* nlines = 3 */
{
printf("%s\n", lineptr[i]);
}
I was wondering if there was a way to read bytes (like this: \x00\x01\x02) from the command line in C.
For example:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%s", argv[1]);
return 0;
}
user#UbuntuServer: ~/Code# gcc Program.c -o Program
user#UbuntuServer: ~/Code# ./Program "\x48\x69"
Hiuser#UbuntuServer: ~/Code# ./Program "\x48\x69\x0a"
Hi
user#UbuntuServer: ~/Code#
Thanks!
Unless you use a library to parse regex strings like that, you'll need to parse the hex manually. Check out this answer (which has slightly different syntax but a similar function):
Hexadecimal string to byte array in C
I would go for something like this:
int main(int argc, char **argv)
{
char *buf = malloc(strlen(argv[1]) / 4 + 1);
size_t i = 0;
for (char *tok = strtok(argv[1], "\\x"); tok; tok = strtok(NULL, "\\x"))
{
sscanf(tok, "%02hhx", buf + i);
i++;
}
buf[i] = '\0';
printf("%s", buf);
free(buf);
return 0;
}
I found the HEX to ASCII conversion functions on this thread, and modified it to suit my situation.
#include <stdio.h>
#include <string.h>
int hexToInt(char c) {
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if(result > 9) {
result--;
}
return result;
}
int hexToASCII(char c, char d) {
int high = hexToInt(c) * 16;
int low = hexToInt(d);
return high + low;
}
int main(int argc, char *argv[]) {
char* hexString = argv[1];
char buf = 0;
for(int i = 0; i < strlen(hexString); i++) {
if(i % 2 != 0) {
printf("%c", hexToASCII(buf, hexString[i]));
} else {
buf = hexString[i];
}
}
return 0;
}
I am new in programming and currently learning on C. Could you please assist me on solving below's case?
An example of this will be if a user is entering "cbamike", I would like to separate it into two strings: cba and mike.
I tried below's code but it doesnt work:
#include <stdio.h>;
int main (int argc, string argv[])
{
char* input[50] = argv[1];
char* first[10];
char* second[10];
sprintf(first, "%c %c %c", input[0], input[1], input[2]);
sprintf(second, "%c %c %c %c", input[3], input[4], input[5], inpput[6]);
printf("%s\n", input);
printf("%s\n", first);
printf("%s\n", second);
}
There is no string in c,
you can use strncpy to get first few characters as aswered there: Strings in c, how to get subString
int main (int argc, string argv[])
{
char* input = argv[1];
char first[4];
char second[5];
strncpy(first, input, 3);
strncpy(second, input + 3, 4);
first[3] = second[4] = '\0';
}
#include <stdio.h>
#include <string.h>
int main (int argc, char* argv[])
{
if(argc >= 2)
{
const int len = strlen(argv[1]) / 2;
char str1[len + 2], str2[len + 2];
snprintf(str1, len + 1, "%s", argv[1]);
snprintf(str2, len + 2, "%s", argv[1] + len);
printf("1: %s\n2: %s\n", str1, str2);
}
return 0;
}
Also after I want to add a key to each letter like 'a' + 1 = 'b'. So I want to take a string for instance "Hello" then do
char 1 = H + 1;
char 2 = E + 1;
etc.
printf("%c" + "%c" + "%c" + "%c" + "%c", 1 , 2 , 3 , 4 , 5);
also I would love for this to be automated because IDK how long the string might be and what key theyre are going to use.
You can do something like this:
#include<stdio.h>
#include<string.h>
int main()
{
char text[] = "Hello";
int i=0;
int size= strlen(text);
for(i=0;i<size;i++)
{
//something here
}
return 0;
}
Assuming the string is mutable, you can update it in place:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void update(char *s, int delta)
{
while(*s)
*s++ += delta;
}
int main(int argc, char **argv)
{
char str[] = "Hello";
update(str, 1);
printf("Encoded: \"%s\"\n", str);
update(str, -1);
printf("Decoded: \"%s\"\n", str);
return 0;
}
If the string is immutable, you will need to make a copy of it, and update the copy.
int main(int argc, char **argv)
{
const char str[] = "Hello";
char *copy = strdup(str);
update(copy, 1);
printf("Encoded: \"%s\"\n", copy);
update(copy, -1);
printf("Decoded: \"%s\"\n", copy);
free(copy);
return 0;
}
You should read about dynamic arrays in C.
#include <stdio.h>
#include <string.h>
char* code(const char* message)
{
int i = 0;
char* coded;
for (i = 0; i < strlen(message); i++)
{
coded[i] = message[i] - 3;
}
return coded;
}
char* decode(const char* message)
{
int i = 0;
char* coded;
for (i = 0; i < strlen(message); i++)
{
coded[i] = message[i] + 3;
}
return coded;
}
int main()
{
// This is dynamic allocated chars array
char* message = "Hello World!";
message = code(message);
printf("%s\n", message);
message = decode(message);
printf("%s\n", message);
}