![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Location: Scotland
Posts: 66
Rep Power: 4
![]() |
Can anybody out there please help me?
For uni module I got to modify a program to enable initial capitalisation no matter what mix of cases is used. #include <stdio.h>
#include <string.h>
#include <iostream.h>
char sname [12];
char fname [12];
char address [25];
char town [25];
int age;
void main()
/* This piece of code is used to gather information from the user. */
{
printf("\nEnter surname: ");
scanf("%s", sname);
printf("\nEnter first name: ");
scanf("%s", fname);
printf("\nEnter age: ");
scanf("%d", &age);
printf("\nEnter address: ");
scanf("%s", address);
printf("\nEnter town: ");
scanf("%s", town);
printf("\n");
putchar(201);
printf(" * * * * * * * * * * * * * * * * ");
putchar(187);
printf("\n");
printf("\n");
printf(" * *");
printf("%s", fname);
printf(" ");
printf("%s", sname);
printf(",");
printf(" ");
printf("Age: %d", age);
printf(",");
printf(" *");
printf("\n");
printf(" * *");
printf("%s", address);
printf(",");
printf("\n");
printf(" * *");
printf("%s", town);
printf(" ");
printf("\n");
printf("\n");
putchar(200);
printf(" * * * * * * * * * * * * * * * * ");
putchar(188);
printf("\n");
}I cannot program for all the sand in the sahara. I need this done immediately and I cannot get coding to work properly. Tried puting in the coding for capitalisation but it locks up and crashes. Thanks, shadowhunter. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
I'm not sure if this is exactly what you need, but it should be close enough to give you the idea of what you should be doing:
#include<stdio.h>
int main() {
char sname[12];
char fname[12];
printf("Enter Surname: ");
scanf("%s", sname);
printf("Enter First Name: ");
scanf("%s", fname);
if(sname[0]>=90) {
sname[0]-=32;
}
if(fname[0]>=90) {
fname[0]-=32;
}
printf("Firstname: %s\nSurname: %s\n\n", fname, sname);
return 0;
}Good luck! Edit: In case you're wondering where the >= 90 and the -30 came from, all the lowercase letters are represented by numbers from 97 to 122, the capitols from 65 to 90. Hence, to get from one to another, you subtract or add 32 (97-65=32) These numeric representations for letters are called ASCII codes. You can check out the ascii character code table here: The ASCII table |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Also, under Ctype.h there is a function called toupper() which makes a character uppercase.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Fixed code tags in the original post.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Here's some simple code to set a word to Title case:
if ((string[0] >= 'a') && (string[0] <= 'z')) {
string[0] -= 32;
}
for (int i = 1; string[i]; i++) {
if ((string[i] >= 'A') && (string[i] <= 'Z)) {
string[i] += 32;
}
} |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
Create a function that accepts a string as a parameter. Inside the function... compare the ascii value of the first position in the string... if not uppercase, do the necessary calculation on the ascii value... using strcat build a new temp string... then return the temp string to the calling function... in this case, it would be the main function right before you wanted to print the results of the names.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
Making a function that builds a temp string seems a bit heavy handed for a little program like this, i think. Though it sure is better coding pratice then when i pretty much just cut-and-pasted. I tell my friends never to cut and paste code but I'm guilty of it too often. Damn being a hypocrit...
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|