Jul 17, 2014 at 4:54am UTC
You could make str big enough to hold any size input expected from the user, or you can use 'new' to dynamically allocate enough memory to store the input.
There are more options if C++ is okay to use.
Jul 17, 2014 at 1:34pm UTC
I recommend using C++, unless there's some reason you have to use C.
1 2
std::string input;
std::cin >> input;
That's all it takes using C++. This also is a lot safer than using C-Style strings.
Last edited on Jul 17, 2014 at 1:35pm UTC
Jul 20, 2014 at 8:04pm UTC
residentbiscuit
C++ is acceptable
do you think you could show how to do it with string
Jul 20, 2014 at 9:01pm UTC
cin >> s;
only gets chars until it finds a space. To get the entire line you should use getline ( cin, s );
. This will get everything until it finds a newline.
Jul 20, 2014 at 9:16pm UTC
1 2 3 4 5 6 7 8 9
#include <stdio.h>
main() {
int c;
for (c = getchar(); c != EOF; c = getchar())
putchar(c | 0x20);
return 0;
}
Last edited on Jul 20, 2014 at 9:22pm UTC
Jul 20, 2014 at 9:50pm UTC
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <iostream>
#include <string>
#include <ctype.h>
#include <string.h>
#include <cctype>
using namespace std;
void ChangeCase(char word[])
{
for (size_t i = 0; i < strlen(word); i++)
{
if (isupper(word[i]))
word[i] = tolower(word[i]);
else
if (islower(word[i]))
word[i] = toupper(word[i]);
}
}
int main ()
{
float val;
cout << "ent " ;
cin >> val;
if (val == 1)
{
cout << "string: " ;
char s[256]; // declaring s array with 80
cin.ignore();
cin.getline (s,256); // input data into named array s
for (int i = 0; i < strlen(s); i++)//
s[i] = tolower(s[i]);
cout <<endl << s << endl;
return 0;
}
if (val == 2)
{
cout << "string: " ;
char s[256]; // declaring s array with 80
cin.ignore();
cin.getline (s,256); // input data into named array s
for (int i = 0; i < strlen(s); i++)//
s[i] = toupper(s[i]);
cout <<endl << s << endl;
return 0;
}
if (val == 3)
{
char s[256];
cin.ignore();
cin.getline(s,256);
ChangeCase(s);
cout << s;
return 0;
}
}
how could i make this do upper case for first and last letter and lowercase in between
Last edited on Jul 20, 2014 at 10:57pm UTC
Jul 21, 2014 at 1:08am UTC
how can i get my code to get input and print output for that fucntion. thank you
Last edited on Jul 21, 2014 at 2:12am UTC