Jul 21, 2014 at 2:10am UTC
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 Jul 21, 2014 at 2:37am UTC
Jul 21, 2014 at 2:36am UTC
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 Jul 21, 2014 at 3:39am UTC
Jul 21, 2014 at 2:39am UTC
how can implement that in my code i understand it logically but not completly understanding how to do it
Jul 21, 2014 at 2:50am UTC
try to do it, and ask me step you stuck.
I'll be here answering you.
Jul 21, 2014 at 3:16am UTC
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 Jul 21, 2014 at 3:17am UTC
Jul 21, 2014 at 3:23am UTC
cause i am a rookie who is struggling at the moment
Jul 21, 2014 at 3:30am UTC
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 Jul 21, 2014 at 3:39am UTC
Jul 21, 2014 at 3:37am UTC
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
Jul 21, 2014 at 3:42am UTC
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 Jul 21, 2014 at 3:42am UTC
Jul 21, 2014 at 3:53am UTC
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 Jul 21, 2014 at 3:56am UTC
Jul 21, 2014 at 3:57am UTC
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 Jul 21, 2014 at 3:58am UTC
Jul 21, 2014 at 4:05am UTC
ok thanks... any recommended reading on a different note? while i try to make this work
Jul 21, 2014 at 4:08am UTC
I think learning about 'char array in C/C++' is good to read and understand.
You can google it, not much hard to understand.
Jul 21, 2014 at 4:11am UTC
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 Jul 21, 2014 at 4:11am UTC
Jul 21, 2014 at 4:13am UTC
Mines is just psudo-code, you change it yourself.
Last edited on Jul 21, 2014 at 4:13am UTC
Jul 21, 2014 at 4:36am UTC
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 Jul 21, 2014 at 4:49am UTC