My program should find any 4 letter words and change them into string "love" ... i have successfully found all the spaces and modified any 4 letter words before the spaces... however, the last word before the '/0' doesn't change... please help.

btw, is there any string class function check for if the the char is a capital letter? thank u

int main()
{
	string sentence;
	
	cout<<"Please enter a sentence."<<endl;
	getline(cin,sentence);
	int startIndex=0, endIndex=0;
	for(int i=startIndex; i<sentence.size(); i++)
	{
		if((sentence[i]==' ')|| (sentence[i]=='/0'))
		{  // i hate you very 
			endIndex=i;
			if((endIndex-startIndex)==5)
			{
				
				sentence.replace(startIndex+1, 4, "love");
			}
			startIndex=i;
		}
		
	}
	
	cout<<sentence;


	return 0;
}

I see you used WaltP's suggestion rather than mine. That's okay. My feelings aren't hurt.:( His suggestion was actually better.

http://www.daniweb.com/forums/thread109017.html

I think you mean

'\0'

rather than

'/0'

You are using a string rather than a C-String, so I wouldn't worry about NULL terminators. I think you may want to check for punctuation rather than the NULL terminator. As for checking for a capital letter, capital letters have ASCII values from 65 to 90 so you can check that way, or you can use the isupper function from cctype. Also from cctype is the ispunct function. Both may be useful.
http://www.cplusplus.com/reference/clibrary/cctype/isupper.html
http://www.cplusplus.com/reference/clibrary/cctype/ispunct.html

First of all, to VernonDozier, I had to use the find() and replace() coz my professor wants us to. But thx, a lotz for replying.

And now, I have a kind of second version using find() and replace(),,, i think it makes sense to me the whole program. I just don't know why nothing came up on screen.

int main()
{
	string sentence;
	
	cout<<"Please enter a sentence."<<endl;
	getline(cin,sentence);
	int spaceOne=0, spaceTwo=0;

	//this is cool right
       // 0123456789
	while(spaceOne<sentence.size())
	{
		spaceOne = sentence.find(' ', spaceOne);  //return the first space  
		spaceTwo= sentence.find(' ', spaceOne);  //return the second space
		if((spaceTwo-spaceOne)==4)       
		{
			sentence.replace(spaceOne, 4, "love");
			spaceOne = spaceTwo;            //set spaceOne = the last space found
			
		}

		else
		{
			spaceOne=spaceTwo;   
		}

	}	
	cout<<sentence;

	return 0;
}
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

int main()
{
    int i,len,t;
    cin>>t;
    char a[100],b[100];
    while(t>0)
    {
                                                
              cin>>a;
              len=strlen(a);len=len-1;
              for(i=0;i<=len;i++)
              {
                                 b[i]=a[len-i];
              }
              cout<<"value of a\n"<<a<<"\n"<<b;
              if(strcmp(a,b)==0)
              cout<<"YES"<<endl;
              else
              cout<<"NO"<<endl;
             
              t--;
    }
    getch();
    return 0;
}

every time the loop runs the garbage value of char b; remains. . .how to clear the old values of b?

start quote:

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

int main()
{
    int i,len,t;
    cin>>t;
    char a[100],b[100];
    while(t>0)
    {

              cin>>a;
              len=strlen(a);len=len-1;
              for(i=0;i<=len;i++)
              {
                                 b[i]=a[len-i];
              }
              cout<<"value of a\n"<<a<<"\n"<<b;
              if(strcmp(a,b)==0)
              cout<<"YES"<<endl;
              else
              cout<<"NO"<<endl;

              t--;
    }
    getch();
    return 0;
}

every time the loop runs the garbage value of char b; remains. . .how to clear the old values of b?

This thread is a year old. Please start a new thread and post your code in code tags:

// paste code here

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.