6 Replies - 822 Views - Last Post: 26 January 2009 - 01:04 PM Rate Topic: -----

#1 goofygoober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Write A program that will asked the user to enter string then change t

Post icon  Posted 26 January 2009 - 06:23 AM

Hi dreamincode members :)

as the title implies i need to write a program that will asked the user to enter string then change the cap of the string "computer", count the number of appearances of the word computer in the entered string.

first i made a if else statement for password verification, then it will come up into a new line that will asked the user to enter a sentence or a phrase. if the word computer is detected in the entered sentence or phrase the computer word will be capitalized.

now this is my problem every time i entered a phrase or sentence every words are capitalized.

ex.
i entered:

i love computer

this is my output:

I LOVE COMPUTER

the output should be:

i love COMPUTER

i still don't know what is the correct code. the program is in string, in strupr. i cant show off the code cause it was made from scratch and i am shy :P ( a begginers code). can someone please enlighten me with these on what code i should be using. thanks a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Write A program that will asked the user to enter string then change t

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 06:33 AM

[rules][/rules]

Shy or not, show your work.
Was This Post Helpful? 0
  • +
  • -

#3 goofygoober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 07:03 AM

ok here's the code, rewritten. i thought i was close but not.



#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>


char str1[225];
char str2[10];
char *ptr;
int x;

void main()
{
 clrscr();

 strcpy(str2,"computer");
 cout<<"enter a phrase or sentence: \n";
 cin>>str1;		
 ptr=strupr(str2);	   // this line here i think
 cout<<str1<<str2;   // and here is wrong
  
 
getch();

}
 



obviously it outputs the computer word even though its not entered. :sighs: i dont know what is the correct code for this.
Was This Post Helpful? 0
  • +
  • -

#4 goofygoober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 07:08 AM

i think there is something like if else statement for the computer word, i dunno. i cant figure this out.
Was This Post Helpful? 0
  • +
  • -

#5 goofygoober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 09

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 09:53 AM

up for this
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 12:17 PM

cout << strupr(str2); will display it in all caps.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Write A program that will asked the user to enter string then change t

Posted 26 January 2009 - 01:04 PM

The function strstr(haystack, needle) will find an instance of the needle (search string) in the haystack (entire string). It returns a pointer to the starting character, or NULL if it fails to find one.

But, as Hyper intimates, you can't use strupr() because that will change the entire string to uppercase.

So, you use strstr to find an instance of the string. Then use pointer arithmetic and the toupper() function until you find a space or the end of the string.
Repeat until you find no more instances of the search string.

EDIT: Alternatively, you can create an upper-case version of your search string, and if you find the search string in the target, then replace it using strncpy.

This post has been edited by JackOfAllTrades: 26 January 2009 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1