How can we store more than 256 characters in a string?

is there any way of storing more than 256 charaters in a string ,if yes how ?? i got this question in viva and didn’t know the answer .

do you know bro @ssjgz
?

That’s a suspiciously weird question. Assuming nothing has gotten lost in translation:

#include <iostream>

using namespace std;

int main()
{
    const string stringWithMoreThan256Characters = string(257, 'X');
    cout << "stringWithMoreThan256Characters has " << stringWithMoreThan256Characters.length() << " characters" << endl;
}

It must mean something else, though, surely?

I guess its related to more than 256 distinct characters, something related to ASCII :eyes::eyes:

1 Like

‘’’#include<bits/stdc++.h>

using namepsace std;

int main()
{
string S;
cout<<"enter string ";
cin>>S;

}
‘’’
here can S store more than 256 characters @ssjgz

characters may be same and may be distint.

Wow!!:joy::joy:

That’s what I was wondering, but if so, it’s very, very ambiguous.

If you want a character set of more than 256 chars, then you need to use another member of the std::basic_string family - std::wstring being the most obvious.

Edit:

Hmmm … although see this answer.

3 Likes

Sorry, are you asking me a question? If so, the answer is “yes” :slight_smile:

(compiler errors aside, of course :))

The only way to know the solution is to contact the teacher :laughing::laughing:

:thinking:

Yes, Ofcourse! We can store more than 256 characters in a string.
#Python
StringOf256chars = input()
print(StringOf256chars)

//C
#include<stdio.h>
#include<string.h>
void main()
{
char StringOf256chars [256];
gets(StringOf256chars ); //read the string
puts(StringOf256chars ); //print the string
}

//JAVA
import java.io. * ;
import java.util.*;
class Strings
{
public static void main(String[]args)
{
Scanner venki=new Scanner(System.in);
String s;
s=venki.nextLine();
System.out.println(s);
}
}

i=input()
print(i)
#python

As always teachers and TA’s ask weird questions in VIVA which has nothing to with programming in general.

3 Likes

very true:sweat_smile::sweat_smile:

1 Like