Insert input string into char str[]

Did anyone know how to insert the input string(characterorintegal) into char str90[]??


1
2
3
4
cout << "Please enter a character or integal" << endl;
     getline(cin, characterorintegal);

	 char str90[] = characterorintegal ;
1
2
3
std::cout << "Please enter a character or integer: ";
std::string input;
std::getline(std::cin, input);
No need to mess with character arrays ;)
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <cstring>

int main()
{
   std::string str = "I am a C++ string!";

   char* cstr = new char[str.length() + 1];
   std::strcpy(cstr, str.c_str());

   std::cout << str << "\n" << cstr << "\n";

   delete[] cstr;
}
Last edited on
but I'm going to use strstr function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>
#include<stdarg.h>
using namespace std;

int main(void)
{
cout << "Please enter a character or integal" << endl;
      string input;
     getline(cin, input);

char str1[]="Hello";

if (strstr(str1, input))
	{
		printf(str1);
	}

	else
	{
		cout << "The character is not in the string1";
	}
	cout << "\n";
}

If an array of characters must be used (say, a modifiable NTBS is required by an API):

1
2
3
4
5
6
7
8
9
std::string characterorintegal ;
std::getline( std::cin, characterorintegal ) ;

constexpr std::size_t MAX_SZ = 90 ;
char cstr[MAX_SZ] ;

// http://en.cppreference.com/w/cpp/string/byte/strncpy
// http://en.cppreference.com/w/cpp/string/basic_string/c_str
std::strncpy( cstr, characterorintegal.c_str(), MAX_SZ-1 ) ; // truncate if input is too long 


Or:
1
2
3
// simpler: get up to MAX_SZ-1 characters directly into cstr
// http://en.cppreference.com/w/cpp/io/basic_istream/getline
std::cin.getline( cstr, MAX_SZ ) ;



> but I'm going to use strstr function

We have std::string::find() http://en.cppreference.com/w/cpp/string/basic_string/find
if( input.find( "Hello" ) != std::string::nops ) { /* found it */ }
Last edited on
but I'm going to use strstr function

Why? Why not use some of the safer C++ string functions to accomplish your objectives.

And note that you can't use a std::string with strstr() or any of the C-string methods.
@jlb
@JLBorges


I figured out how to solve it...
The reason I want to use strstr function is because I need to search multiples (30 strings) using a string input(key in by user)

1
2
3
4
5
//just need to replace 
getline(cin, input);
//with
char str90[31];
	cin.get(str90, 31); // str90 is my cin, 31 is the no.of elements in the string(so it doesn't matter)// 


so the final program should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>
#include<stdarg.h>
using namespace std;

int main(void)
{
cout << "Please enter a character or integal" << endl;
      string input;
     char str90[31];
	cin.get(str90, 31);

char str1[]="Hello";

if (strstr(str1, str90))
	{
		printf(str1);
	}

	else
	{
		cout << "The character is not in the string1";
	}
	cout << "\n";
}
Like I asked before, why not use the much safer C++ strings instead of the C-string?

Perhaps something like?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Please enter a character or integal: " ;
    string input;
    getline(cin, input);

    string str1 = "Hello big fellow";
    size_t location;

    if((location = str1.find(input)) != std::string::npos)
    {
       cout << str1.substr(location) << '\n';
    }
    else
    {
       cout << "The character is not in the string1\n";
    }
}

1
2
Please enter a character or integal: big
big fellow
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/195899/
Topic archived. No new replies allowed.