![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 6
| help simple string outputting ex. Input name: dante lopez Your name is Dante Lopez I have a problem in making the first letter of the last name into upper case. Here is my C code: Code: #include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main()
{
int x=0, mm,dd,yyyy,ctr,i,count;
char name[25],choice;
clrscr();
puts("Input your name: ");
gets(name);
count=strlen(name);
//name[25]=tolower(name[25]);
name[0]=toupper(name[0]);
for(x=0;name[x]<=count;x++)
{
i = isspace (name[x]);
if(i==1)
name[x++]=toupper(name[x++]);
}
printf("Input your birthday [mm/dd/yyyy]: ");
scanf("%d/%d/%d",&mm,&dd,&yyyy);
printf("\n\n");
puts(name);
if(mm==01)
printf("January %d %d",dd,yyyy);
else if(mm==02)
printf("February %d %d",dd,yyyy);
else if(mm==03)
printf("March %d %d",dd,yyyy);
else if(mm==04)
printf("April %d %d",dd,yyyy);
else if(mm==05)
printf("May %d %d",dd,yyyy);
else if(mm==06)
printf("June %d %d",dd,yyyy);
else if(mm==07)
printf("July %d %d",dd,yyyy);
else if(mm== 8)
printf("August %d %d",dd,yyyy);
else if(mm== 9)
printf("September %d %d",dd,yyyy);
else if(mm==10)
printf("October %d %d",dd,yyyy);
else if(mm==11)
printf("November %d %d",dd,yyyy);
else if(mm==12)
printf("December %d %d",dd,yyyy);
else
printf("Month should be 1-12");
printf("\n\nWould still like to input [y/n]: ");
scanf("%s",&choice);
getch();
}
|
| kingdm is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 3,144
| OK, what will you always have to help mark the second name? How about a space - as in the space between the first and second name. And we know that the first letter after the space, will be the very letter that you want to capitalize. So, use strchr() to find the first space, and then increment the pointer one time, and then capitalize that letter with toupper(). Or, you can scan through the name, from 0 to the index whose element in the array holds the space value (' '), or 32 - either one it's the same. So whichever way you want to do it, it will be done in approximately the same way. |
| Adak is offline | |
| | #3 |
| Registered User Join Date: Nov 2009
Posts: 6
| @Adak thanks for the reply. Yes the space is the identifier for the last name. how will i use the strchr(), where will i put it? sorry i'm only new to this C language. Thanks. |
| kingdm is offline | |
| | #4 |
| Registered User Join Date: Sep 2006
Posts: 3,144
| If you're new than you want to do it yourself, just to get the experience: in a for loop, let's walk through the char array: Code: for(i = 0; yourArray[i] != 32; i++); //Note the semi-colon on the end - this is generally *wrong*, but this for loop needs //to do nothing else, for us. Because of differences in compilers and computers, yourArray[i] may have gone one too far, and already be at the first letter of the last name. Just FYI. |
| Adak is offline | |
| | #5 |
| Registered User Join Date: Nov 2009
Posts: 6
| I can't seem to understand the strchr(). I'm having trouble in this string manipulation, can you please fix my loop part in my code. |
| kingdm is offline | |
| | #6 |
| Registered User Join Date: Nov 2009
Posts: 6
| @Adak I've changed my loop part into this: Code: for(x=0;name[x]!=' ';x++)
{
if(name[x]==' ')
name[x+1]=toupper(name[x+1]);
}
|
| kingdm is offline | |
| | #7 |
| Registered User Join Date: Nov 2009
Posts: 6
| Can somebody help me please.... |
| kingdm is offline | |
| | #8 | |
| Registered User Join Date: Sep 2006
Posts: 3,144
| Quote:
Here's an example, in a program. Code: #include <stdio.h>
#include <ctype.h>
int main(void) {
char name[25] = "samuel adams";
int i;
for(i = 0; name[i] != 32; i++);
name[0] = toupper(name[0]);
name[i] = toupper(name[++i]);
printf("\n\n %s", name);
printf("\n\n\t\t\t press enter when ready");
i = getchar();
return 0;
}
| |
| Adak is offline | |
| | #9 |
| Registered User Join Date: Jan 2008
Posts: 281
| Code: /* Get a pointer to the location of the space character */ char *ptr = strchr(name, ' '); /* Make sure you actually found a space character */ if (ptr != NULL) ptr[1] = toupper(ptr[1]); Code: for(i = 0; name[i] != 32; i++); |
| arpsmack is offline | |
| | #10 |
| Registered User Join Date: Jan 2009
Posts: 221
| Then you could add: Code: for(i = 0; name[i] != ' ' && i < sizeof(name); i++); Last edited by Subsonics; 11-08-2009 at 01:04 PM. |
| Subsonics is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 6
| Thanks a lot to all who replied, got the program working. Adak thanks a lot. |
| kingdm is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unable to compare string with 'getter' returned string. | Swerve | C++ Programming | 2 | 10-30-2009 05:56 PM |
| RicBot | John_ | C++ Programming | 8 | 06-13-2006 06:52 PM |
| Simple problem with string... Please take a look | SG57 | C Programming | 9 | 06-03-2006 06:48 AM |
| Calculator + LinkedList | maro009 | C++ Programming | 20 | 05-17-2005 12:56 PM |
| Linked List Help | CJ7Mudrover | C Programming | 9 | 03-10-2004 10:33 PM |