Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
10-22-2019, 06:27 PM
#1
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
add quotes to both side of a char variable, how to? C
I am giving up on trying to figure out (for now anyways).
How to add a double quote to each side of an already existing variable into another or same named variable to use it later on in a different program.
this program is my "testing program" to figure out what I need before moving it all into the other one.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char fixcolors[145][21]={
"Alice Blue",
"Antique White",
"Aqua",
"Aquamarine",
"Azure",
"Beige",
"Bisque",
"Black",
"Blanched Almond",
"Blue",
"Blue Violet",
"Brown",
"Burlywood",
"Cadet Blue",
"Chartreuse",
"Chocolate",
"Coral",
"Cornflower Blue",
"Cornsilk",
"Crimson",
"Cyan",
"Dark Blue",
"Dark Cyan",
"Dark Goldenrod",
"Dark Gray",
"Dark Green",
"Dark Khaki",
"Dark Magenta",
"Dark Olive Green",
"Dark Orange",
"Dark Orchid",
"Dark Red",
"Dark Salmon",
"Dark Sea Green",
"Dark Slate Blue",
"Dark Slate Gray",
"Dark Turquoise",
"Dark Violet",
"Deep Pink",
"Deep Sky Blue",
"Dim Gray",
"Dodger Blue",
"Firebrick",
"Floral White",
"Forest Green",
"Fuchsia",
"Gainsboro",
"Ghost White",
"Gold",
"Goldenrod",
"Gray",
"Web Gray",
"Green",
"Web Green",
"Green Yellow",
"Honeydew",
"Hot Pink",
"Indian Red",
"Indigo",
"Ivory",
"Khaki",
"Lavender",
"Lavender Blush",
"Lawn Green",
"Lemon Chiffon",
"Light Blue",
"Light Coral",
"Light Cyan",
"Light Goldenrod",
"Light Gray",
"Light Green",
"Light Pink",
"Light Salmon",
"Light Sea Green",
"Light Sky Blue",
"Light Slate Gray",
"Light Steel Blue",
"Light Yellow",
"Lime",
"Lime Green",
"Linen",
"Magenta",
"Maroon",
"Web Maroon",
"Medium Aquamarine",
"Medium Blue",
"Medium Orchid",
"Medium Purple",
"Medium Sea Green",
"Medium Slate Blue",
"Medium Spring Green",
"Medium Turquoise",
"Medium Violet Red",
"Midnight Blue",
"Mint Cream",
"Misty Rose",
"Moccasin",
"Navajo White",
"Navy Blue",
"Old Lace",
"Olive",
"Olive Drab",
"Orange",
"Orange Red",
"Orchid",
"Pale Goldenrod",
"Pale Green",
"Pale Turquoise",
"Pale Violet Red",
"Papaya Whip",
"Peach Puff",
"Peru",
"Pink",
"Plum",
"Powder Blue",
"Purple",
"Web Purple",
"Rebecca Purple",
"Red",
"Rosy Brown",
"Royal Blue",
"Saddle Brown",
"Salmon",
"Sandy Brown",
"Sea Green",
"Seashell",
"Sienna",
"Silver",
"Sky Blue",
"Slate Blue",
"Slate Gray",
"Snow",
"Spring Green",
"Steel Blue",
"Tan",
"Teal",
"Thistle",
"Tomato",
"Turquoise",
"Violet",
"Wheat",
"White",
"White Smoke",
"Yellow",
"Yellow Green"
};
#define ARRAY_SIZE( array ) ( sizeof( array ) / sizeof( array[0] ) )
char * randomColors(void)
{
int upper = ARRAY_SIZE(fixcolors);
int i = 0, num = 0, lower = 1, count = 1;
for (i = 0; i < count; i++) {
num = (rand() %
(upper - lower + 1)) + lower;
}
return fixcolors[ num ];
}
int main(void)
{
char *color = NULL;
printf("Number of rows: %d\n", ARRAY_SIZE(fixcolors));
printf("Number of columns: %d\n", ARRAY_SIZE(fixcolors[0]));
/*
for (int i = 0; i<ARRAY_SIZE(fixcolors);i++)
{
printf("%s\n",fixcolors[i]);
}
for (int i=0;i<10;i++)
printf("%s\n", printRandoms () );
*/
color = strdup ( randomColors() );
strcat(color, "\"");
printf("%s\n", color);
return 0;
}
strcat is right side, how do I get a quote on the left side?
I seen a strrev that is suppose to be in string.h but not in this one.
https://www.programmingsimplified.co...reverse-string
Code:
blue
//to be
"blue"
in the variable itself so printf gets me
Code:
printf("%s\n",color);
"blue"
Last edited by BW-userx; 10-22-2019 at 06:30 PM .
10-22-2019, 06:33 PM
#2
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
Original Poster
Never mind I tried putting them into the array again using a smaller one.
Code:
char new[2][20] = { "\"gog\"", "\"we\""};
it was giving me greegree in the larger one, let me go do it again.
10-22-2019, 06:41 PM
#3
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
can you not just
Code:
printf("\"%s\"\n", color);
this is how I do it in bash
but yeah , having " as part of the string in the array would also work
but that is usually something I would avoid
10-22-2019, 06:49 PM
#4
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
Original Poster
Quote:
Originally Posted by
Firerat
can you not just
Code:
printf("\"%s\"\n", color);
this is how I do it in bash
but yeah , having " as part of the string in the array would also work
but that is usually something I would avoid
yeah but no, I need them in side of the var. so I did this with a listings of colors to save time adding them one at a time.
example file of colors without quotes
Code:
Alice Blue
Antique White
Aqua
Aquamarine
Azure
Beige
Bisque
Black
Blanched Almond
Blue
Blue Violet
Brown
Burlywood
Cadet Blue
script I wrote to put them into a different file with the quotes.
Code:
#!/usr/bin/env bash
#19 long
#amount 145
c=0
while read f
do
echo "\"\\\"$f\\"\"\"","
((c++))
done <$1
echo $c
from the cli
Code:
./qouteme Colors-X11 >> qColors
results
Code:
"\"Alice Blue\"",
"\"Antique White\"",
"\"Aqua\"",
"\"Aquamarine\"",
"\"Azure\"",
"\"Beige\"",
"\"Bisque\"",
"\"Black\"",
"\"Blanched Almond\"",
"\"Blue\"",
"\"Blue Violet\"",
fixed.
adding them results into that array in the c program,
Code:
....
int main(void)
{
color = strdup ( randomColors() );
printf("%s\n", color);
.... gets this
$ ./a.out
Number of rows: 145
Number of columns: 21
"Blue"
Last edited by BW-userx; 10-22-2019 at 06:52 PM .
10-22-2019, 07:26 PM
#5
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
ahh
ok
this is one of those situations were I have never understood how(why) this works
Code:
while read foo
do
printf "%s\n" "$foo"
done < <(
for i in foo bar
do
printf "\\\\\"%s\\\\"\n" "$i"
done
)
you think you need two, but five is the magic
10-22-2019, 07:30 PM
#6
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
Original Poster
Quote:
Originally Posted by
Firerat
ahh
ok
this is one of those situations were I have never understood how(why) this works
Code:
while read foo
do
printf "%s\n" "$foo"
done < <(
for i in foo bar
do
printf "\\\\\"%s\\\\"\n" "$i"
done
)
you think you need two, but five is the magic
Code:
echo "\"\\\"$f\\"\"\"","
yeah it is, escape the quote to leave a quote, then escape the escape to leave an escape then escape the quote that quotes the var, then escape the escape to leave an escape then escape the quote to leave a quote, then a quote to escape the escape then then add a comma while making sure you got quotes around all of it to keep it together.
It does get confusing. for me anyways. I just kept adding them until I seen the results I needed.
Code:
"\"Blanched Almond\"",
Last edited by BW-userx; 10-22-2019 at 07:40 PM .
10-22-2019, 07:35 PM
#7
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
yeah, that what I do
but I think I'm missing something
Code:
foo="\"foo\"";printf "I get this %s\n" "$(printf "%s" "$foo")"
I get this "foo"
Code:
foo="\"foo\"";printf "I get this %s\n" "$foo"
I get this "foo"
as far as I understand it, printf in c and bash work pretty much the same
Last edited by Firerat; 10-22-2019 at 07:37 PM .
10-22-2019, 07:45 PM
#8
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
Original Poster
Quote:
Originally Posted by
Firerat
yeah, that what I do
but I think I'm missing something
Code:
foo="\"foo\"";printf "I get this %s\n" "$(printf "%s" "$foo")"
I get this "foo"
Code:
foo="\"foo\"";printf "I get this %s\n" "$foo"
I get this "foo"
as far as I understand it, printf in c and bash work pretty much the same
with escapes, i guess.
If you what to add whatever to a string you have to escape it, the \ and quotes, for sure.
if you want to add an escape \ you have to escape it. \\
Code:
$ foo="\"\\"\"foo\\"\"\",";printf "I get this %s\n" "$(printf "%s" "$foo")"
I get this "\"foo\"",
----
"\"Blanched Almond\"",
that way is actually better it works too in that script of mine.
Code:
#!/usr/bin/env bash
#19 long
#amount 145
c=0
while read f
do
#"\"\\"\"foo\\"\"\","
#echo "\"\\\"$f\\"\"\"","
echo "\"\\"\"$f\\"\"\","
((c++))
done <$1
echo $c
Last edited by BW-userx; 10-22-2019 at 07:55 PM .
10-22-2019, 08:59 PM
#9
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
turns out you need to define the string length
so
Code:
{
char color[22] = "\"";
printf("Number of rows: %d\n", ARRAY_SIZE(fixcolors));
printf("Number of columns: %d\n", ARRAY_SIZE(fixcolors[0]));
strcat(color, randomColors() );
strcat(color, "\"");
printf("%s\n", color);
return 0;
}
how good/safe this is I have no idea, I have no clue about c
10-22-2019, 09:03 PM
#10
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
Original Poster
Quote:
Originally Posted by
Firerat
turns out you need to define the string length
so
Code:
{
char color[22] = "\"";
printf("Number of rows: %d\n", ARRAY_SIZE(fixcolors));
printf("Number of columns: %d\n", ARRAY_SIZE(fixcolors[0]));
strcat(color, randomColors() );
strcat(color, "\"");
printf("%s\n", color);
return 0;
}
how good/safe this is I have no idea, I have no clue about c
long story short, it turns out I did not need the quotes after all, I was thinking on cli it does if it is two words or more off the cli, but sending it within the program it didn't work, so I put it back without quotes, now I am running it in my system.
on cli it has to be
Code:
-color "bright red"
that was my line of thought.
All times are GMT -5. The time now is 01:38 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News