string from user input

how could i make it so this works on a user inputted string instead of 'test string'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <ctype.h>
int main ()
{

    int i=0;
    char str[]="Test String.\n";
    char c;
        while (str[i])
{
            c=str[i];
            putchar (tolower(c));
            i++;
}
  return 0;
}
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.
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
residentbiscuit

C++ is acceptable
do you think you could show how to do it with string
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>
#include <ctype.h>
#include <string.h>
using namespace std;

main ()
{
float val;
cout << "ent";
cin >> val;
if (val == 1)
{
        cout << "string: ";
        char s[80];  // declaring s array with 80
        cin.get(s, 80); // 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
        
}
}


why does this skip the cin.get ??
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>
#include <ctype.h>
#include <string.h>
using namespace std;

main ()
{
float val;
cout << "ent";
cin >> val;
if (val == 1)
 {
        cout << "string: ";
        char s[256];  // declaring s array with 80
        cin >> s; // 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;
         
 }
}


why does this not handle a sentence
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.
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
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
this function will take the string and convert to first and last letters to upper case and the middle letters to lower case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void convertFirstAndLastLetter(string &str)
{
    size_t last = strlen(str) - 1;
    size_t first = 0;
    
    str[first] = toupper(str[first]);
    str[last] = toupper(str[last]);

    for(int = first + 1 ; i < last - 1; i++)
    {
        str[i] = tolower(str[i]);
    }
}

how can i get my code to get input and print output for that fucntion. thank you
Last edited on
Topic archived. No new replies allowed.