Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 09-22-2005, 07:57 PM   #1 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
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
josh2two is offline   Reply With Quote
Old 09-23-2005, 12:58 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 09-23-2005, 01:05 AM   #3 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
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.
Locutus is offline   Reply With Quote
Old 09-23-2005, 01:12 AM   #4 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
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.
Locutus is offline   Reply With Quote
Old 09-23-2005, 01:27 AM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to 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. :-)
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 09-23-2005, 01:47 AM   #6 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
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.
Locutus is offline   Reply With Quote
Old 09-23-2005, 07:02 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote
Old 09-23-2005, 09:17 AM   #8 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
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");
josh2two is offline   Reply With Quote
Old 09-23-2005, 09:54 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-23-2005, 02:08 PM   #10 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
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");
josh2two is offline   Reply With Quote
Old 09-24-2005, 08:40 AM   #11 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
@redhead: I suppose it's a matter of style, but you could of course just do
Code:
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Locutus is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey guys! Code malfunction - need experts workover groundfrog Platform/API C++ 1 06-09-2005 07:46 AM
how the servlet will integrate the LDAP code j.gohel Java 19 04-16-2005 12:55 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM


All times are GMT -8. The time now is 09:30 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting