Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}
// Part 3
//1. loop through the string looking for ". "
//2. when ". " is found, delete one of the spaces
//3. Repeat process until ". " is not found.
string forceSingleSpaces1 (string str) {
size_t found(str.find(". "));
while (found !=string::npos){
str.erase(found+1,1);
found = str.find(". ");
}
return str;
}
int main(){
cout << forceSingleSpaces1("sentence1. sentence2. end. ") << endl;
return EXIT_SUCCESS;
}#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include<stdio.h>
#include <string.h>
using namespace std;
int doubledelete();
int numberwordsinstring();
int reversewloop();
int pass()
int main();
int reverse();
int asciistring();
int asciiarray();
int getput();
// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}
//Part 2 Repeater
std::string repeat( const std::string &word, int times ) {
std::string result ;
result.reserve(times*word.length()); // avoid repeated reallocation
for ( int a = 0 ; a < times ; a++ )
result += word ;
return result ;
}
int main( ) {
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}
// Part 3
// loop through the string looking for ". "
// when ". " is found, delete one of the spaces
// Repeat process until ". " is not found.
string forceSingleSpaces1 (string str) {
size_t found(str.find(". "));
while (found !=string::npos){
str.erase(found+1,1);
found = str.find(". ");
}
return str;
}
int doubledelete(){
cout << forceSingleSpaces1("sentence1. sentence2. end. ") << endl;
return EXIT_SUCCESS;
}
// Part 4 number of words in string
int numberwordsinstring()
{
int i, numspaces;
char nextChar;
string msg;
numspaces=1;
cout << "Type in a string\n";
getline(cin, msg);
// checks each character in the string
for (i=0; i<int(msg.length()); i++)
{
nextChar = msg.at(i); // gets a character
if (isspace(msg[i]))
numspaces++;
}
cout << "\nThere are " << numspaces << " words in this string.";
cin.ignore();
return 0;
}
// Part 6 Reverse
char STOP[265];
void reverse(char* a)// error: initializing argument 1 of ‘int reverse(char*)’ [-fpermissive]
{
int c = strlen(a) - 1;
for (; c >= 0; c--)
{
cout << a[c];
}
}
int reversewloop()
{
cout<<"Please digit an input to be reversed: "<<endl;
cin.getline(STOP,265);
reverse(STOP); //error: invalid conversion from ‘char’ to ‘char*’
// [-fpermissive]
system("pause");
return 0;
}
//7. Reverse no loops
int complexReverseString(string userInput)
{
string source(userInput);
string target( source.rbegin(), source.rend() );
cout << "The reversed string is " << target << endl;
return 0;
}
int reverse()
{
ifstream input;
string forward;
cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
input.open("information.txt");
cin >> forward;
//reverseString(forward, findStringSize(forward)); a function I'm not actually calling
input >> forward;
complexReverseString(forward);
input.close();
system ("pause");
}
//8 Ascii
//C program to accept a string from user and
//display its ascii value and
//then display sum of all ascii value of strings
int asciistring() {
char String[100];
int Sum,Index;
Sum=0; //Sum is initially zero
printf("Enter the string:\n");
gets(String); //Accept String from User
for(Index=0;Index<strlen(String);Index++)
{
Sum+=(String[Index]); //Adds (the ASCII values of) the String characters.
}
printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value.
return 0;
}
//9 acsiiArraysum
//10 getput
int pass()
cout << "\t\tEnter Username : "; //asking to enter username
cin >> User;
cout << "\t\tEnter Password : "; //asking to enter password
do
{
ch = getch();
if(sizeof(password) != 0)
{
if( ch == '\\b')
{
putch('\\b');
password.erase(password.size() - 1, 1);
continue;
}
}
else if(ch <= '9' && ch >= '0' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') //If user enters 1-9, a-z, or A-Z, add it to the password and display an asterisk
{
password += ch;
putch('*');
}
}
while {ch == '\\r');int asciiarray()
using std::cout;
using std::endl;
int ASCIIsumOfString(string s){
int sum = 0;
for(int i=0; i<s.length(); i++){
sum += s[i];
}
return sum;}
int ASCIIsumOfStringArray(string *s, int numberOfStrings){
int sum = 0;
for(int i=0; i<numberOfStrings; i++){
sum += ASCIIsumOfString(s[i]);
}
return sum;
}
int getput()
{
string ????;
fstream streamObject("http://www.gutenberg.org/cache/epub/1342/pg1342.txt");
fstream localFile("pap.txt");
fstream newFile("new.txt");
if(streamObject.fail())
cout << "file failed to load." << endl;
else
cout << "file load was successful." << endl;
if(localFile.fail())
cout << "file2 failed to load." << endl;
else
cout << "file load was successful." << endl;
while(!streamObject.eof())
{
streamObject.put(localFile.get());
}
streamObject.close();
char single = localFile.get();
while(!localFile.eof())
{
???? += single;
single = localFile.get();
}
localFile << newFile;
localFile.close();
asciistring();
doubledelete();
numberwordsinstring();
return 0;
}
return 0;
}#include <conio.h>
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
std::string getPassword()
{
std::string password = "";
char c = getch();
while (c != '\n' && c != '\r') {
if(c==8) {
if(password.length()>0) {
password = password.substr(0, password.length() - 1);
cout << "\b \b";
}
} else {
password += c;
cout << "*";
}
c=getch();
}
cout << endl;
return password;
}#include <iostream>
using namespace std;
int ASCIIsumOfString(string s){
int sum = 0;
for(int i=0; i<s.length(); i++){
sum += s[i];
}
return sum;}
int ASCIIsumOfStringArray(string *s, int numberOfStrings){
int sum = 0;
for(int i=0; i<numberOfStrings; i++){
sum += ASCIIsumOfString(s[i]);
}
return sum;
}#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include<stdio.h>
#include <string.h>
using namespace std;
int main()
{
int doubledelete();
int repeat();
int numberwordsinstring();
int reverseloop;
int reversenoloop();
int asciistring();
int acsiiArraysum();
int getput();
}
// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}
//Part 2 Repeater
std::string repeat( const std::string &word, int times ) {
std::string result ;
result.reserve(times*word.length()); // avoid repeated reallocation
for ( int a = 0 ; a < times ; a++ )
result += word ;
return result ;
}
int repeat() {
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}
// Part 3
// loop through the string looking for ". "
// when ". " is found, delete one of the spaces
// Repeat process until ". " is not found.
string forceSingleSpaces1 (string str) {
size_t found(str.find(". "));
while (found !=string::npos){
str.erase(found+1,1);
found = str.find(". ");
}
return str;
}
int doubledelete(){
cout << forceSingleSpaces1("sentence1. sentence2. end. ") << endl;
return EXIT_SUCCESS;
}
// Part 4 number of words in string
int numberwordsinstring()
{
int i, numspaces;
char nextChar;
string msg;
numspaces=1;
cout << "Type in a string\n";
getline(cin, msg);
// checks each character in the string
for (i=0; i<int(msg.length()); i++)
{
nextChar = msg.at(i); // gets a character
if (isspace(msg[i]))
numspaces++;
}
cout << "\nThere are " << numspaces << " words in this string.";
cin.ignore();
return 0;
}
// Part 6 Reverse
char STOP[265];
void reverse(char* a)// error: initializing argument 1 of ‘int reverse(char*)’ [-fpermissive]
{
int c = strlen(a) - 1;
for (; c >= 0; c--)
{
cout << a[c];
}
}
int reverseloop()
{
cout<<"Please digit an input to be reversed: "<<endl;
cin.getline(STOP,265);
reverse(STOP); //error: invalid conversion from ‘char’ to ‘char*’
// [-fpermissive]
system("pause");
return 0;
}
//7. Reverse no loops
int complexReverseString(string userInput)
{
string source(userInput);
string target( source.rbegin(), source.rend() );
cout << "The reversed string is " << target << endl;
return 0;
}
int reversenoloop()
{
ifstream input;
string forward;
cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
input.open("information.txt");
cin >> forward;
//reverseString(forward, findStringSize(forward)); a function I'm not actually calling
input >> forward;
complexReverseString(forward);
input.close();
system ("pause");
}
//8 Ascii
//C program to accept a string from user and
//display its ascii value and
//then display sum of all ascii value of strings
int asciistring() {
char String[100];
int Sum,Index;
Sum=0; //Sum is initially zero
printf("Enter the string:\n");
gets(String); //Accept String from User
for(Index=0;Index<strlen(String);Index++)
{
Sum+=(String[Index]); //Adds (the ASCII values of) the String characters.
}
printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value.
return 0;
}
//9 acsiiArraysum
using std::cout;
using std::endl;
int acsiiArraysum()
{
const int myArrayNumberOfElements(5);
double myArray[myArrayNumberOfElements] = {1.1, 4.5, 5.7, 7.9, 10};
double sum(0);
for (int i(0); i < myArrayNumberOfElements; i++)
{
sum +=myArray[i];
}
cout << endl << "Sum of all elements: " << sum << endl;
system("PAUSE");
return 0;
}
//10 getput
int getput()
{
string strdata = "pap.txt";
fstream streamObject("pap.txt");
fstream localFile("pap.txt");
fstream newFile("new.txt");
FILE *filepointer;
int character;
filepointer=fopen("pap.txt", "r"); /* filepointer points to data.txt */
if (filepointer==NULL) { /* error opening file returns NULL */
printf("Could not open data.txt!\n"); /* error message */
return 1; /* exit with failure */
}
/* while character is not end of file */
while ((character=fgetc(filepointer)) != EOF) {
putchar(character); /* print the character */
}
fclose(filepointer); /* close the file */
return 0; /* success */
}1>------ Build started: Project: string, Configuration: Debug Win32 ------
1>Build started 4/27/2012 6:12:39 PM.
1>InitializeBuildStatus:
1> Touching "Debug\string.unsuccessfulbuild".
1>ClCompile:
1> strings.cpp
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(15): warning C4101: 'reverseloop' : unreferenced local variable
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(158): warning C4996: 'gets': This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> e:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(277) : see declaration of 'gets'
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(160): warning C4018: '<' : signed/unsigned mismatch
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(199): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> e:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(144): error C4716: 'reversenoloop' : must return a value
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.36
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
int main()
{
int doubledelete();
int repeat();
int numberwordsinstring();
int reverseloop;
int reversenoloop();
int asciistring();
int acsiiArraysum();
int getput();
}Users browsing this forum: Google [Bot] and 1 guest