Making the last letter of a char uppercase?

Hi, i need help in making the last letter of "bob" Uppercase so it will print out "boB" any ideas, i think im so close but im not sure

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
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>

using namespace std;

int main(){
  {
int i=0;
char Name[] = "bob";
char d;
cin >> Name;
while (Name[i])
  {
    d=Name[i];
    if (d = ""){
    i--;
    putchar (toupper(d));
    }

cout <<"Please Enter your name to Generate a password\n";

cout << "Password = " << Name.length() << Name[0] << Name[i]<<endl;

     }
return 0;
}

  }
Last edited on
closed account (o3hC5Di1)
Hi there,

If you must use C-string instead of std::string (the latter being preferred), you could use the strlen() function:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cctype>
#include <cstring>

int main()
{
   char c[] = "Michael";
   
   c[strlen(c)-1] = toupper(c[strlen(c)-1]);
   
   std::cout << c;
}
MichaeL


I'm not sure why you have a newline character at the back of your string, but you should probably keep that in mind, as it will serve as the last character of the string (not including the \0 character, of course).

All the best,
NwN
Thanks that really helped alot, one more thign though, how do i get it so i just prints out the "L" in "michaeL"?
closed account (o3hC5Di1)
You could use the same technique:

std::cout << c[strlen(c)-1];

I would urge you to use std::string though: http://www.cplusplus.com/reference/string/string/

All the best,
NwN
Thanks so much, i got it working now :D and ill look at that link just now thanks again :)
Topic archived. No new replies allowed.