Converting text to First Letter Capitalisation
Moderators: chulett, rschirm, roy
Converting text to First Letter Capitalisation
In good old Server Edition, in the derivation of a transformer stage, you just did an OCONV using the MCT conversion To Produce Text With First Letter Capitalisation.
Anyone know of a nice simple way of doing this in Enterprise Edition?
Anyone know of a nice simple way of doing this in Enterprise Edition?
_______________________________________
"If I had asked people what they wanted they would have said faster horses"
Henry Ford
"If I had asked people what they wanted they would have said faster horses"
Henry Ford
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Premium Member
- Posts: 1735
- Joined: Thu Mar 01, 2007 5:44 am
- Location: Troy, MI
You can use the following parallel routine to do what you want.
I don't have the compiler now hence its not tested. Its just an idea to achive what you want.
Code: Select all
#include<iostream.h>
#include<ctype.h>
char* ConvMCT(Char *str)
{
int Space=1
While(*str)
{
If(Space=1 and isLower(*str))
{
*str=toupper(*str)
}
If(int(*str)=32)
{
Space=1
}
else
{
Space =0
}
str++
}
}
Priyadarshi Kunal
Genius may have its limitations, but stupidity is not thus handicapped.
Genius may have its limitations, but stupidity is not thus handicapped.
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
Obviously you have not tested or checked the code. So not intended to point mistakes.
Few changes required in the code
1.) look for non-alpha as seperators. So rather than restricting check for blank, look for anything between 'A-Z' or 'a-z' and do an 'else' capitalisation.
2.) Need return of char pointer
3.) Need to store initial location so to return
4.) Semicolons and cases
Few changes required in the code
1.) look for non-alpha as seperators. So rather than restricting check for blank, look for anything between 'A-Z' or 'a-z' and do an 'else' capitalisation.
2.) Need return of char pointer
3.) Need to store initial location so to return
4.) Semicolons and cases
-
- Premium Member
- Posts: 1735
- Joined: Thu Mar 01, 2007 5:44 am
- Location: Troy, MI
I didn't had the compiler hence I was unable to validate anything. That was just an idea. Below is the modified one. Haven't tested this one also but incorporated the points made by you.Sainath.Srinivasan wrote:Obviously you have not tested or checked the code. So not intended to point mistakes.
Few changes required in the code
1.) look for non-alpha as seperators. So rather than restricting check for blank, look for anything between 'A-Z' or 'a-z' and do an 'else' capitalisation.
2.) Need return of char pointer
3.) Need to store initial location so to return
4.) Semicolons and cases
Code: Select all
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "ctype.h"
char* ConvMCT(char *str) //Function with string input and string
{
char *result = (char *)malloc (sizeof(char *));
int x=0, Flag=1; // Setting Flag to 1 to make the first letter capital.
char CheckStr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while(*str)
{
if(Flag=1) //Check if the last character was not alphabet.
{
if(isalpha(*str) and islower(*str)) //Convert to uppercase if its a lower case alphabet.
{
result[x] = upper(*str);
}
else
{
result[x] = *str; //No Change if its already in uppercase or not an alphabet.
}
}
else
{
if(isalpha(*str) and isupper(*str))
{
result[x] = tolower(*str); //Convert to lowercase except the first character.
}
else
{
result[x] = *str;
}
}
if(!strchr(CheckStr, *str)) //Check if the string is not a-z and A-Z.
{
Flag=1;
}
else
{
Flag =0;
}
++x;
++str;
}
result[x] = '\0'; //Terminate the string
return result; //Return the replaced string
}
Priyadarshi Kunal
Genius may have its limitations, but stupidity is not thus handicapped.
Genius may have its limitations, but stupidity is not thus handicapped.
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
You are doing a good job !!
Couple of minor points.
1.) If you are doing malloc, you need malloc.h (if most compilers)
2.) Result is assigned to a random character pointer. Don't you think it is better to do a direct "result = str" so they point to the same place!!?
This way you can use 'x' for both rather than using pointers.
Couple of minor points.
1.) If you are doing malloc, you need malloc.h (if most compilers)
2.) Result is assigned to a random character pointer. Don't you think it is better to do a direct "result = str" so they point to the same place!!?
This way you can use 'x' for both rather than using pointers.
-
- Premium Member
- Posts: 1735
- Joined: Thu Mar 01, 2007 5:44 am
- Location: Troy, MI
The malloc function is already included in stdlib.h Hence no need to explicitly include malloc.h. May be some compilers doesn't have that (Never encountered one). In that case include malloc.hSainath.Srinivasan wrote:Couple of minor points.
1.) If you are doing malloc, you need malloc.h (if most compilers)
It would be better if result is assigned to str and then only update the required letters but if we have to check each and every character and converting the first one to uppercase and others to lowercase the performance will be similar.Sainath.Srinivasan wrote:2.) Result is assigned to a random character pointer. Don't you think it is better to do a direct "result = str" so they point to the same place!!?
This way you can use 'x' for both rather than using pointers.
but you can get rid of
else
{
result[x] = *str;
}
block. and number of lines will reduce.
However its on the person who wants to use it.
Anyways thanks for you valuable suggestion. I will try to post make a new version of it with this comment incorporated but I don't want to post another one without compiling and testing the code.
Thanks again.
Priyadarshi Kunal
Genius may have its limitations, but stupidity is not thus handicapped.
Genius may have its limitations, but stupidity is not thus handicapped.
-
- Premium Member
- Posts: 296
- Joined: Sun Nov 16, 2008 7:41 pm