How to Store Multiple Strings as One Variable

How do I store multiple strings as one variable

Supposing that you want to check for the username and password corresponding to that username, python provides structure called dictionary. May this will be helpful to you.

userPass = {"Jake": "Hello", "Bake": 'notem'} #dictonary named userPass, "username": "Password"

user_name = input ("Hello, user please enter your username!")
Password = input("Please enter your password %s" %user_name)

if userPass[user_name] == Password: #userPass[user_name] will return password defined in dictonary
print ("Welcome back %s" %user_name)
else:
print ("You are an imposter! Begone!!")

Storing multiple strings In C

Let's start from the beginning.

I don't think you really understand what is char though. Char is only ONE character. So things like char array[16][1] mean that you have array of 16 strings where each string have maximum length of one. Also your next step scanf("%s", &array[i][j]); doesn't make sense, since you're getting multiple symbols as an input, but you're writing only to single character. Proper solution would be something like this:

char array[a][255]; // 255 will be maximum length of one 'string'
for(int i = 0; i < a; ++i) {
scanf("%s", array[i]);
}

As you can see, you don't need & sign here, because array[i] already returns address of the first character in the string. The same thing applies to printing. Proper way is to do following thing:

for(int i = 0; i < a; ++i) {
printf("%s\n", array[i]);
}

Your solution only displays one character.

And remember, char is just basic number, ranging typically from 0-255 (if compiler defaults char to unsigned char). Your code implies that you treat char as a full C++ string, which it definitely isn't.

How can I store multiple variable datas in a string/array

You can use the sprintf function to write data to a character string:

//...
char dataString[11]; // Enough space for DD-MM-YYYY plus the required nul-terminator
sprintf(dateString, "%02d-%02d-%04d", getDate.dd, getDate.mm, getDate.yyyy);

The %02d format specifies that 2 digits should be printed, adding a leading zero if the value is < 10.

C - how to store multiple strings in an array

You can use 2D array to store multiple strings. For 10 strings each of length 100

char variable[10][100];

printf("Enter Strings\n");
for (int i = 0; i < 10 ;i++)
scanf("%100s", variable[i]);

Better to use fgets to read string.

fgets(variable[i], sizeof(variable[i]), stdin);  

You can also use dynamic memory allocation by using an array of pointers to char.

What is the syntax for adding multiple strings to one variable in Java?

It's

String[] history = new String[] { "hey", "you", "Mom" };

Multiple strings in one variable js

You can check using indexOf. indexOf() returns -1 when the supplied value isn't present

var commands = ['command1', 'commands2']
commands.indexOf(input.innerHTML.toLowerCase()) > -1

var input = document.getElementById("cmd");
var output = document.getElementById("demo");

var commands = ['command1', 'commands2'];

input.addEventListener("input", function() {
var result = null;

// indexOf() returns -1 when the supplied value isn't present
if(commands.indexOf(cmd.value.toLowerCase()) > -1){
// your logic to change color, or do whatever
result = "yes";
} else {
result = "no";
}
output.textContent = result;

});
<input id="cmd">

<p id="demo"></p>

How to store multiple string values into one string in C?

you can use sprintf for coping multiple string to one string with your own format also.

sprintf(destination_string, "%s----%s", source_string1,source_string2);

Storing lots of different String in one variable

If you want to store Unique multiple String , Then I guess java.util.Set is a good option

Set<String> uniqueString = new HashSet<String>();

I hope below example will help you to understand the concept better

    Set<String> uniqueString = new HashSet<String>();
uniqueString.add("test");
uniqueString.add("test");
uniqueString.add("count");
uniqueString.add("check");

for (String item : uniqueString) {
System.out.println(item);
}

Output

check

count

test

Here you can find(in output) , duplicate string "test" has been striped out and only unique values has been printed



Related Topics



Leave a reply



Submit