changing case for first and last letter

i need ThE FirsT AnD LasT letter of every word to be uppercase and the rest lowercase but function convertFirstAndLastLetter only does the first and last of the sentence.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
#include <string>
#include <ctype.h>
#include <string.h>
#include <cctype>
using namespace std;

void convertFirstAndLastLetter(char wrd[])
{
    size_t last = strlen(wrd) - 1;
    size_t first = 0;
    
    wrd[first] = toupper(wrd[first]);
    wrd[last] = toupper(wrd[last]);

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


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;
 }
     if (val == 4)
 {
        char wrd[256];
        cin.ignore();
        cin.getline(wrd,256);
        convertFirstAndLastLetter(wrd);
        cout << wrd;

  return 0;
 }

}


Last edited on
1. change everything to lower
2. change [first] and [last] to upper
3. find ' ' in the string , assume position is x
4. change [x-1] and [x+1] to upper

done.
Last edited on
how can implement that in my code i understand it logically but not completly understanding how to do it
try to do it, and ask me step you stuck.
I'll be here answering you.
um i have gone wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void convertFirstAndLastLetter(string& wrd)
{   
    
    size_t last = strlen(wrd) - 1;
    size_t first = 0;
    
    wrd[first] = toupper(wrd[first]);
    wrd[last] = toupper(wrd[last]);
    
    for(int a = 0; i < strlen(s); i++)// 
            s[a] = tolower(s[a]);
    
    for(int i = first + 1 ; i < last - 1; i++)
    {
        wrd[i] = toupper(wrd[i]);
    }
}
1
2
    for(int a = 0; i < strlen(s); i++)// 
            s[a] = tolower(s[a]);


infinity loop, you used i++ with a vars.

1
2
3
4
for(int i = first + 1 ; i < last - 1; i++)
    {
        wrd[i] = toupper(wrd[i]);
    }


what is this line for ?

Why don't you try to use step I suggest ?
Last edited on
cause i am a rookie who is struggling at the moment
Okay, so.
Something like this.


1. change everything to lower

1
2
3
for i=0 -> i=last {
    wrd[i] = tolower(wrd[i]);
}


2. change [first] and [last] to upper

1
2
wrd[first] = toupper(wrd[first])
wrd[last] = toupper(wrd[last])


3. find ' ' in the string , assume position is x
4. change [x-1] and [x+1] to upper

1
2
3
4
5
6
7
int x
for x=0 -> x=last{
    if( wrd[x] == ' '){
        wrd[x-1] = toupper(wrd[x-1])
        wrd[x+1] = toupper(wrd[x+1])
    }
}


BTW, you have to try to understand some, there'll be better.
Last edited on
that is very true currently looking for tutor. i appreciate everything i really do.

im going to try what you gave me abd let you know
this works with char?
What 'this' ?
BTW, I'm not sure about your tolower() and toupper(), I like to use
X = X-'a'+'A' and X = X-'A'+'a' more -..-
Last edited on
no not 'this' the code your sharing with me char works it doesnt have to be string?
and your X = X- 'a' +'A' you do that for every letter?
Last edited on
The string is just array of char,
just process EVERY CHAR in the STRING.

As I understand your wrd is your string, right?
I just process the CHAR in it.
Last edited on
ok thanks... any recommended reading on a different note? while i try to make this work
use this to split your sentence http://www.cplusplus.com/reference/cstring/strtok/
then process each words separately
I think learning about 'char array in C/C++' is good to read and understand.

You can google it, not much hard to understand.
compiling errors

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <ctype.h>
#include <string.h>
#include <cctype>
using namespace std;

void convertFirstAndLastLetter(char wrd[])
{
    size_t last = strlen(wrd) - 1;
    size_t first = 0;
    
    int i
    for i=0 -> i=last {
    wrd[i] = tolower(wrd[i]);
    }
    wrd[first] = toupper(wrd[first]);
    wrd[last] = toupper(wrd[last]);
    

    int x
    for x=0 -> x=last{
    if( wrd[x] == ' '){
        wrd[x-1] = toupper(wrd[x-1])
        wrd[x+1] = toupper(wrd[x+1])
    }
}
	

}


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;
 }
     if (val == 4)
 {
        char wrd[256];
        cin.ignore();
        cin.getline(wrd,256);
        convertFirstAndLastLetter(wrd);
        cout << wrd;

  return 0;
 }

}
Last edited on
Mines is just psudo-code, you change it yourself.
Last edited on
i c. still banging away
i cant figure it out still i have been going at this for 12+ straight hours. burnt out... thank you for everything tonight i think i need to give it a rest though. once again thank you and please check back tomorrow as i will be working on this still.
Last edited on
Topic archived. No new replies allowed.