|
 |
|
 |
09-22-2005, 07:57 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 22
|
Having major trouble figuring out this code!!!!!!!!
I can't figure out how to do this code, Please help
Enter a string: hello
After capitalizing the first letter of this string it becomes: Hello
After capitalizing the last letter of this string it becomes HellO
After adding three to the first letter of this string it becomes KellO
After swapping the first and last letters of this string it becomes OellK
|
|
|
09-23-2005, 12:58 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Um that doesn't look like code to me. Is that the output you are supposed to write a program to produce? Remember char variables in C/C++ can be treated as ints.
__________________
Stop intellectual property from infringing on me
|
|
|
09-23-2005, 01:05 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 20
|
C or C++?
Also, you probably want to be a bit more specific as to what you've got so far and what you still need.
|
|
|
09-23-2005, 01:12 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 20
|
Quote:
|
Originally Posted by teknomage1
Remember char variables in C/C++ can be treated as ints.
|
Carefull there... in a typical C++ implementation these days a char is 8 bits wide while an int is 32 bits. And that's not even guaranteed to be true.
|
|
|
09-23-2005, 01:27 AM
|
#5 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
True but I'm assuming he's working on an exercise using char* arrays and is supposed to use addition functions to manipulate the characters. Since he's only adding 3, I doubt the variable will overflow. :-)
__________________
Stop intellectual property from infringing on me
|
|
|
09-23-2005, 01:47 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 20
|
Quote:
|
Originally Posted by teknomage1
True but I'm assuming he's working on an exercise using char* arrays and is supposed to use addition functions to manipulate the characters. Since he's only adding 3, I doubt the variable will overflow. :-)
|
Adding only one can be enough to overflow a variable  Not that that is necessarily a bad thing for this particular excercise. Although you probably don't want \0 characters in the middle of a string, the post doesn't really say what to do when you add 3 to for example 'z'. My point would be that you should simply treat chars as chars.
C uses zero terminated arrays of chars, in C++ std::string is preferred. In C++ you'd use C++ streams with std::cin to get input, in C you have to read stuff from stdin, etc.
Just need more info.
|
|
|
09-23-2005, 07:02 AM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Josh2two, experiment with this:
Code:
#include <iostream>
#include <string>
int main()
{
std::string theWord = "hello";
theWord[0] = theWord[0]+3;
std::cout<<theWord<<std::endl;
return 0;
}
__________________
|
|
|
09-23-2005, 09:17 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 22
|
it has to do these operations to whatever the user enters:
THIS IS WHAT I HAVE SO FAR
char message[50];
cout<<"Enter a string: ";
cin>> message;
cout<<"After capitalizing the first letter of this string it becomes: ";
message[0] = toupper (message [0]);
cout<<message;
cout<<"\n""After capitalizing the last letter of this string it becomes: ";
cout<<message;
cout<<"\n""After adding three to the first letter of this string it becomes: ";
cout<<message;
cout<<"\n""After swapping the first and last letters of this string it becomes: ";
cout<<message<<"\n""\n";
system("PAUSE");
system("CLS");
|
|
|
09-23-2005, 09:54 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Here is one in C
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LENGTH 256
int main()
{
char alphabet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
char message[LENGTH +1], temp;
int i;
printf("Enter a string: ");
fflush(stdout);
fgets(message, LENGTH, stdin);
message[strlen(message) -1] = '\0'; /* remove '\n' from string */
message[0] = toupper(message[0]);
printf("After capitalizing the first letter of this string it becomes: %s\n",
message);
message[strlen(message) -1] = toupper(message[strlen(message) -1]);
printf("After capitalizing the last letter of this string it becomes: %s\n",
message);
for(i=0; i < alphabet[i]; ++i)
if(message[0] == alphabet[i])
{
message[0] = alphabet[(i+3)%strlen(alphabet)];
break;
}
printf("After adding three to the first letter of this string it becomes: %s\n",
message);
temp = message[0];
message[0] = message[strlen(message) -1];
message[strlen(message) -1] = temp;
printf("After swapping the first and last letters of this string it becomes: %s\n",
message);
return 0;
}
But since you're looking at C++, you might want to use some nifty string functions.
|
|
|
09-23-2005, 02:08 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 22
|
thanks so much everyone, this is the final code
char alphabet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
char message[LENGTH +1], temp;
int i;
cout<<"Enter a string: ";
cin>>message;
message[0] = toupper(message[0]);
cout<<"After capitalizing the first letter of this string it becomes: "<< message<<"\n";
message[strlen(message) -1] = toupper(message[strlen(message) -1]);
cout<<"After capitalizing the last letter of this string it becomes: "<< message<<"\n";
for(i=0; i < alphabet[i]; ++i)
if(message[0] == alphabet[i])
{
message[0] = alphabet[(i+3)%strlen(alphabet)];
break;
}
cout<<"After adding three to the first letter of this string it becomes:"<< message<<"\n";
temp = message[0];
message[0] = message[strlen(message) -1];
message[strlen(message) -1] = temp;
cout<<"After swapping the first and last letters of this string it becomes:"<< message<<"\n";
system("PAUSE");
system("CLS");
|
|
|
09-24-2005, 08:40 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 20
|
@redhead: I suppose it's a matter of style, but you could of course just do
Code:
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:30 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|