LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-22-2019, 06:27 PM   #1
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
 
Old 10-22-2019, 06:33 PM   #2
BW-userx
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
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
 
Old 10-22-2019, 06:41 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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
 
Old 10-22-2019, 06:49 PM   #4
BW-userx
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
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Firerat View Post
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.
 
Old 10-22-2019, 07:26 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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
 
Old 10-22-2019, 07:30 PM   #6
BW-userx
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
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Firerat View Post
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.
 
Old 10-22-2019, 07:35 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.
 
Old 10-22-2019, 07:45 PM   #8
BW-userx
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
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Firerat View Post
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.
 
Old 10-22-2019, 08:59 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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
 
Old 10-22-2019, 09:03 PM   #10
BW-userx
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
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Firerat View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Browse Websites Side by Side Using Side View Feature on Mozilla Firefox Web Browser | Test Pilot LXer Syndicated Linux News 0 06-08-2018 06:05 PM
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 11:17 AM
char variable is behaving as unsigned char manohar Programming 3 02-15-2011 12:19 AM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration