dimanche 19 avril 2015

Removing punctuation and capitalizing in C

I'm writing a program for school that asks to read text from a file, capitalizes everything, and removes the punctuation and spaces. The file "Congress.txt" contains



(Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.)


It reads in correctly but what I have so far to remove the punctuation, spaces, and capitalize causes some major problems with junk characters. My code so far is:



void processFile(char line[]) {
FILE *fp;
int i = 0;
char c;

if (!(fp = fopen("congress.txt", "r"))) {
printf("File could not be opened for input.\n");
exit(1);
}

line[i] = '\0';
fseek(fp, 0, SEEK_END);
fseek(fp, 0, SEEK_SET);
for (i = 0; i < MAX; ++i) {
fscanf(fp, "%c", &line[i]);
if (line[i] == ' ')
i++;
else if (ispunct((unsigned char)line[i]))
i++;
else if (islower((unsigned char)line[i])) {
line[i] = toupper((unsigned char)line[i]);
i++;
}
printf("%c", line[i]);
fprintf(csis, "%c", line[i]);
}

fclose(fp);
}


I don't know if it's an issue but I have MAX defined as 272 because that's what the text file is including punctuation and spaces.


My output I am getting is:



C╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Press any key to continue . . .

How can I add two strings that contain integer to find the sum?

I try to find the sum of two strings without having to convert to integer like using atoi or stoi It is BigInt class implementation


Example of two strings:



BigInt a("-1000");
BigInt b("+999");


I have a class which private members are:



private:
string data;
bool negative;


The purpose of this question is to write a arithmetic operations like +,-,/,*


A way to replace strings in javascript

I am looking for the best way to replace strings in javascript. Only problem, I need it replaced like this:


Here's my string: The brown fox jumps over the brown fence


I need javascript to find two words like this: The **brown** fox jumps over the **brown** fence


And I need to replace the string with what is in the middle of those words: fox jumps over the


I've been looking online but I cant find anything.


Any idea?


Storing The Contents Of A AutoCompleteSource (HistoryList) In a String

All I am simply trying to do is display the AutoCompleteSource.HistoryList in a message box. All the following code does is display the words HistoryList. How can I make it actually show the URLS of the HistoryLIst, in other words I want the contents of the HistoryList to appear in the MessageBox.



string s = AutoCompleteSource.HistoryList;
MessageBox.Show(s, "History", MessageBoxButtons.OK, MessageBoxIcon.Information);

Writing code for Shift Cypher in C#

My class lab has us write a Shift Cypher and Sub Cypher methods that are called in a button click event. Below you will find the code for this problem.


Can someone help me out with the two methods in question and also calling those methods in the event handler?


enter code here



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace String_Stuff
{
public partial class stringForm : Form
{
public stringForm()
{
InitializeComponent();
}

private string SwitchCase(string input)
{
string str1 = input;
string output = str1.ToUpper();

return output;

}


private string Reverse(string input)
{
string str1 = input;
char[] inputArray = input.ToCharArray();
Array.Reverse(inputArray);
string output = new string(inputArray);

return output;
}

private string PigLatin(string input)
{
// rules: words that begin with a consinant-
//is moved to the end and "ay" is added.
// words that begin with a vowel-
// the sylable "ay" is added to the end of the word.
// e.g. eagle -> eagleay, apple->appleay.
string str1 = input;
string ay = "ay";
input = inputTextBox.Text; // already a string, no need to convert
input = input.Substring(1, input.Length - 1) + input.Substring(0, 1);
str1 = " the word in Pig Latin is " + input + ay; // need to use Text property to display string
string output= str1 ;
return output;
}



private string ShiftCypher(string input, int shift)
{
//Each letter in the word is replaced by a letter some fixed number of positions down the alphabet.
//For this lab, shift by 3. Example: program, shifted by 3 will become surjudp

string output = null;
char[] A = null;
A = input.ToCharArray();
int temp;
for (int i = 0; i < input.Length; i++)
{
temp = (int) (A[i] + shift);
output += (char)temp;
}


return output;
}

private string SubCypher(string input, string charsToSub)

{
//Each letter in the word will be replaced by a letter from the corresponding position
//in a substitution alphabet. For this lab use this substitution alphabet: zeroabcdfghijklmnpqstuvwxy.
//Example: disk would become ofqh.
string subAlpha = "zeroabcdfghijklmnpqstuvwxy";
string str1 = input;
string subCypher = subAlpha;



string output = "";
return output;
}



private void transformButton_Click_1(object sender, EventArgs e)
{
string input = inputTextBox.Text;

switchCaseTextBox.Text = SwitchCase(input);
reverseTextBox.Text = Reverse(input);
pigLatinTextBox.Text = PigLatin(input);
shiftTextBox.Text = ShiftCypher(input,shift);
subTextBox.Text = SubCypher(input, charsToSub);
}
}
}

fgets does not prompt for user input. What is the difference?

I have two scenarios using fgets below. Both scenarios are in the same file, as shown below.



struct sth
{
char str[10];
int num;
};

void getIt(struct sth **M){
char *b;
(*M)=malloc(sizeof(struct sth));

printf("give me an integer:");
fgets(b,1,stdin); // output must be an address
(*M)->num = atoi(b);

printf("give me a string:");
fgets((*M)->str,10,stdin);

}


int main(int argc, char const *argv[])
{
struct sth *myThing;
getIt(&myThing);
printf("Heres the string %s\n", myThing->str);
printf("Heres the num \n", myThing->num);
return 0;
}


Here is the output. Notice that it does not prompt the user for the integer, it just prints "give me an integer", and then moves directly on to the next print statement. Why does it do this?



give me an integer:give me a string:sdf
Heres the string sdf

Heres the num


This small problem is a larger problem in a bigger problem, so this is just a microcosm of the larger one.


Split String and Do Calculation in MySQL

Let's say I have given data as string:



+----------+
|Size |
+----------+
|15X10 |
|5X4 |
|3 |
|2X6X5 |
+----------+


I want to write this column as integer like this:



+----------+
|Size |
+----------+
|150 |
|20 |
|3 |
|60 |
+----------+


Some of them are multiply of three numbers, some of them just one number. I can split the string but cannot make MySQL to calculate the number. Thank you.