Help Needed Please!!!!!

This is a discussion on Help Needed Please!!!!! within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a small problem that I need to fix. I have char array which has capital letters spread ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Help Needed Please!!!!!

    Hi,

    I have a small problem that I need to fix. I have char array which has capital letters spread throughout them . I want to be abe to scan through the array and everytime i see a capital letter insert a space before it . Is this possible .

    Can anyone help ??

    Thanks a lot

    Jon

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    I am not totally sure!!!

    I have not tried this myself, but in my mind this would work...
    Otherwise we will see some complaints below wont we!!

    something like this...

    for (i = 0;textArray[i] != '\0';i++)
    {
    if ( toupper (textArray[i]))
    {
    cout << textArray[i];
    }
    }

    this is just in my mind though, as I said, I have never tried it!!
    This may just make all the character as capital letter, and then this just tests if that is possible....

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    try something like this:

    Code:
    int numCaps = 0;
    
    for(int i = 0; i < strlen(sentence); i++)
    {
       if(isupper(sentence[i]))
      {
          numCaps++;
      }
    }
    
    char* newSentence = new char[numCaps + strlen(sentence) + 1];
    char* cp = newSentence;
    
    for(i = 0; i < strlen(sentence); i++)
    {
       if(isupper(sentence[i]))
      {
          *(cp++) = ' ';
      }
      *(cp++) = sentence[i];
    }
    
    strncpy(sentence, newSentence, sizeof(sentence));
    delete newSentence;
    NOTE: this code wasn't compiled... so if it doesn't work, then try and fix it yourself.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use a loop to check each element of the char array from element[0] and < element[strlen(string)]. check each element with isupper() as indicated in previous post. if isupper() is true then shift all elements of char array from element[strlen(string)] to current element[index] to right by one (eg, element[index + 1] = element[index]). then assign space to element[index]. Be sure there is enough space in char array to avoid overwriting array bounds.

    Easier still is to use the insert() method of vectors, if you have/are allowed the luxury.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Talking Sorry about my last post!!!

    Most things that gets into my mind the first time usually donīt work, so here is some code that actually works!!

    Eaven though it may not be the best example..

    #include <iostream>
    using namespace std;

    int main()
    {
    const int uc_start = 65;
    const int uc_end = 90;
    const int size = 1000;
    char bText[size] = "Hello There, You Supreme C++ Programmer";
    char upperCase[size];
    int i = 0,j = 0;

    for (i = 0;bText[i] != '\0';i++)
    {
    if ( (int)bText[i] >= uc_start && (int)bText[i] <= uc_end )
    {
    upperCase[j] = bText[i];
    j++;
    }

    }
    upperCase[j] = '\0';
    cout << endl;
    cout << "The UpperCase characters in bText[] is: " << flush;
    cout << upperCase << endl;

    return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    I remembered that I forgot!!

    I forgot the code tags!!

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const int uc_start = 65;	// upper case chars starts at this (int) value
    	const int uc_end = 90;		// upper case chars starts at this (int) value
    	const int size = 1000;
    	char bText[size] = "Hello There, You Supreme C++ Programmer";
    	char upperCase[size];
    	int i = 0,j = 0;
    
    	for (i = 0;bText[i] != '\0';i++)
    	{
    		if ((int)bText[i] >= uc_start && (int)bText[i] <= uc_end)
    		{
    			upperCase[j] = bText[i];
    			j++;
    		}
    
    	}
    	upperCase[j] = '\0';
    	cout << endl;
    	cout << "The UpperCase characters in bText[] is: " << flush;
    	cout << upperCase << endl;
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Talking Well...

    Well part of it is correct anyways, not bad I think...
    I have a habit of quickly reading trough messages, so I usually end up doing something like this...Bummer

  8. #8
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Dude, Vinterwolf, check out the edit button on the bottom right of each of your posts

    The main idea is to creatively use the isupper() function in ctype.h (or cctype for your ANSI kidz ) and shifting to do this. The problem is to create an array that will be able to adequately hold all these new spaces.

    Uraldor has the right idea here. vVv's code will work (and I admire it) but it might be hard for some people to understand if they don't understand the ASCII system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. Releasing a program - what is needed? A few Q's
    By ulillillia in forum Tech Board
    Replies: 9
    Last Post: 04-28-2007, 12:18 AM
  5. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21