Printing first letter of string

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 23
Reputation: th3learner is an unknown quantity at this point 
Solved Threads: 1
th3learner th3learner is offline Offline
Newbie Poster

Printing first letter of string

 
0
  #1
Oct 23rd, 2009
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. void main()
  6. {
  7. char s[100],z[10];
  8. clrscr();
  9. cout<<"Enter your name:";
  10. gets(s);
  11. cout<<strupr(s[0]);
  12. cout<<".";
  13. getch();
  14. }

what I am looking for is that when user input deniweb,then it should output D
please help

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 45
Reputation: StaticX is an unknown quantity at this point 
Solved Threads: 0
StaticX StaticX is offline Offline
Light Poster
 
0
  #2
Oct 23rd, 2009
If its a c string you could just print out the first element in the array

  1. cout << your_array[0]; // prints out first element of array
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: th3learner is an unknown quantity at this point 
Solved Threads: 1
th3learner th3learner is offline Offline
Newbie Poster
 
0
  #3
Oct 23rd, 2009
Originally Posted by StaticX View Post
If its a c string you could just print out the first element in the array

  1. cout << your_array[0]; // prints out first element of array
yea i know...but i want first letter should be capital

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 45
Reputation: StaticX is an unknown quantity at this point 
Solved Threads: 0
StaticX StaticX is offline Offline
Light Poster
 
-1
  #4
Oct 23rd, 2009
Oh,well you could write a series of if/else if statements like

  1. if(my_array[0]=='a' || 'A')
  2. cout << "A";
  3. else if(my_array[0]=='b' || 'B')
  4. cout << "B";

But that would be extremely long winded
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #5
Oct 23rd, 2009
Does this work?

  1. cout<<strupr(s[0]).substring(0,1);
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: th3learner is an unknown quantity at this point 
Solved Threads: 1
th3learner th3learner is offline Offline
Newbie Poster
 
0
  #6
Oct 24th, 2009
Originally Posted by StaticX View Post
Oh,well you could write a series of if/else if statements like

  1. if(my_array[0]=='a' || 'A')
  2. cout << "A";
  3. else if(my_array[0]=='b' || 'B')
  4. cout << "B";

But that would be extremely long winded
Tried , but not works

Input------------>daniweb
Output---------->Ddaniweb



Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #7
Oct 24th, 2009
  1. cout<<strupr(s[0]);
This Does Not Work . Because strupr() takes a char* as an argument. So the argument should be a sting having a null character ie: '\0' at the end. But you give a character as its argument. This is the error.

To solve this problem, You can do two things.
First, You can use the function toupper()
But you will need an additional header file #include <ctype.h> or if you have a modern compiler #include <cctype>
Or else if you wish to use strupr() itself, heres a little snippet..

  1. char temp[2];
  2. temp[0]=s[0];
  3. temp[1]='\0';
  4. cout<<strupr(temp);
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 360 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC