You are on page 1of 6

Programming Notes

Rafael Guimarães, Federal Institute of Paraíba March 27, 2021

Problem 1

If you need to get all line on input with char array [C++]

Listing 1: To get all line on C++


1 #include <iostream>
2 #include <string>
3
4 int main () {
5 char string[1002];
6 getchar(); // cleaning the buffer
7 std::cin.getline(string,1002);
8 }

Problem 2 [strings]

if you need to convert string to numerical types;

Listing 2: Convertings
1 char num[10] = "100";
2 int valor = atoi(num);

• atof() from string to float

• atoi() from string to int

• atol() from string to long

• itoa() from int to string

• ltoa() from long to string

Page 1
Problem 3

Have the function FirstReverse(str) take the str parameter being passed and return the string in
reversed order.

1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 string FirstReverse(string str) {
7 char aux;
8 for(int i = 0; i < str.length()/2; i++) {
9 aux = str[i];
10 str[i] = str[str.length()-1-i];
11 str[str.length()-1-i] = aux;
12 }
13 return str;
14 }
15
16 int main(void) {
17 // keep this function call here
18 cout << FirstReverse(gets(stdin));
19 return 0;
20
21 }

Problem 4

Have the function FirstFactorial(num) take the num parameter being passed and return the
factorial of it.

1 #include <iostream>
2 using namespace std;
3
4 long FirstFactorial(short const num) {
5 return num <= 1 ? 1 : num*FirstFactorial(num-1);
6 }
7
8 int main() {
9 cout << FirstFactorial(gets(stdin));
10 return 0;
11 }

Page 2
1 Tabela ASCII

Figure 1: Tabela ASCII

Problem 5

Given a string "This is a dacing sentence" your program shoud send out "ThIs Is A dAnCiNg
SeNtEnCe" this is a problem 1234, of URI online judge;

Page 3
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 char upper(char lower) {
6 return lower - 32;
7 }
8 char lower(char upper) {
9 return upper + 32;
10 }
11 string dacing (string sentence) {
12 string dacingSetence;
13 bool flag = false;
14 for (int i = 0; i < sentence.length(); i++ ) {
15 if (sentence[i] == ’ ’) {
16 dacingSetence.push_back(sentence[i]);
17 continue;
18 }
19 if (isalpha(sentence[i]) && !flag) {
20 if (sentence[i] >= ’a’ && sentence[i] <= ’z’) {
21 dacingSetence.push_back(upper(sentence[i]));
22 } else {
23 dacingSetence.push_back(sentence[i]);
24 }
25 flag = true;
26 } else {
27 if (sentence[i] >= ’A’ && sentence[i] <= ’Z’) {
28 dacingSetence.push_back(lower(sentence[i]));
29 } else {
30 dacingSetence.push_back(sentence[i]);
31 }
32 flag = false;
33 }
34 }
35 return dacingSetence;
36 }
37 int main() {
38 string line;
39 getline(cin, line);
40 cout << dacing(line) << endl;
41 return 0;
42 }

Page 4
Problem 6

When we need a sub string from other string;

1 #include <iostream>
2 #include <string>
3
4 int main(){
5
6 std::string mystring = "Dia 5";
7
8 // begin at index 4;
9 std::cout << mystring.substr(4) << "\n";
10
11 // begin at index 0, finish at index 2.
12 std::cout << mystring.substr(0,3) << "\n";
13
14 }

Output:

5
Dia

Problem 7

Given two numbers, you should find the greatest common divisor.

1 #include <iostream>
2
3 int gcd(int a, int b) {
4 if (b == 0) return a;
5 return gdc(b, a%b);
6 }

Problem 8

Given a number n ≥ 1, ou should to find the sequence sum, from 1 to n. if n = 4 the answer is
10, Because,
1 + 2 + 3 + 4 = 10

Page 5
1 #include <iostream>
2
3 long long sequenceSum (int n) {
4 return n*(n-1)>>1;
5 }

Page 6

You might also like