C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2009, 11:14 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 6
help simple string outputting

Can anyone help me, we have this assignment. A program that will ask for the full name (first name and last name) and the birthday. then the program will output the first letter of full name and last name in capital.

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();
}
Hope someone can help me...
kingdm is offline   Reply With Quote
Old 11-08-2009, 11:25 AM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,501
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   Reply With Quote
Old 11-08-2009, 11:28 AM   #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   Reply With Quote
Old 11-08-2009, 11:37 AM   #4
Registered User
 
Join Date: Sep 2006
Posts: 2,501
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.
Now yourArray[i] is your space between names, so capitalize the yourArray[i+1], and you're on to the next problem.

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   Reply With Quote
Old 11-08-2009, 11:48 AM   #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   Reply With Quote
Old 11-08-2009, 11:56 AM   #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]);
}
Still no luck
kingdm is offline   Reply With Quote
Old 11-08-2009, 12:01 PM   #7
Registered User
 
Join Date: Nov 2009
Posts: 6
Can somebody help me please....
kingdm is offline   Reply With Quote
Old 11-08-2009, 12:44 PM   #8
Registered User
 
Join Date: Sep 2006
Posts: 2,501
Quote:
Originally Posted by kingdm View Post
@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]);
}
Still no luck
You can't change the for loop logic around, and say "no luck". I gave you the logic, and the code - just use it, as I gave it to you.

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   Reply With Quote
Old 11-08-2009, 12:48 PM   #9
Registered User
 
Join Date: Jan 2008
Posts: 276
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]);
Keep in mind that this
Code:
for(i = 0; name[i] != 32; i++);
is dangerous if the input string doesn't have a space character in it.
arpsmack is offline   Reply With Quote
Old 11-08-2009, 01:01 PM   #10
Registered User
 
Join Date: Jan 2009
Posts: 151
Then you could add:

Code:
for(i = 0; name[i] != ' ' && i < sizeof(name); i++);
Assuming name is an array of chars, if the array size is defined as a constant sizeof() could be replaced with that.

Last edited by Subsonics; 11-08-2009 at 01:04 PM.
Subsonics is offline   Reply With Quote
Old 11-08-2009, 01:15 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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