void correcter(string s, int j)
{
string correct;
for (; j < s.length(); j++)
{
if (int(s[j]) != 46){
if (int(s[j]) >= 97 && int(s[j]) <= 122 && i == 0)
{
char a = int(s[j]) - 32;
correct += a;
i++;
}
else if (int(s[j]) >= 65 && int(s[j]) <= 90&&i==0)
{
char a = int(s[j]) + 32;
correct += a;
i++;
}
else if (int(s[j]) >= 65 && int(s[j]) <= 90)
{
char a = int(s[j]) + 32;
correct += a;
i++;
}
else
correct += s[j];
}
else
{
correct += ". ";
i = 0;
}
}
cout << correct << endl;
}
question is to write a code that convert first character of string to uppercase and other stay as lowercase. after every "." make the words first char again upper and other parts in lower!
Input:
hellOWOrLD.hELLOWORLD.
Output:
Helloworld. Helloworld.
It should work like in the picture...
I would use isalpha(), toupper() and tolower()
EDIT: To look for punctuation.
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void upperCase(string& line) {
bool firstCharacter = true;
string punctuation=".?!";
for(int i = 0; line[i] != '\0'; i++) {
char c=line[i];
if(isalpha(c)) {
if (firstCharacter) {
line[i] = toupper(c);
firstCharacter = false;
} else {
line[i] = tolower(c);
}
} else if (punctuation.find(c)!=string::npos) {
firstCharacter=true;
}
}
}
int main() {
string str = "hello UNiverse?! World? Hello. Hello";
upperCase(str);
std::cout << str << '\n';
}
Related
Wrote a function that calculates the average length of words in a sentence.
Why does the program print 0 instead of the average?
Please help me fix my mistake.
If you know how to make an implementation in one function, please write.
#include <iostream>
#include <string>
using namespace std;
int CountWordsAndLetters(char* str, int& words, int& letters)
{
words = 0;
int i = 0;
letters = 0;
while (str[i] == ' ')
i++;
for (; str[i]; i++) {
if (((str[i] >= 'a') && (str[i] <= 'z'))
|| ((str[i] >= 'A') && (str[i] <= 'Z')))
letters++;
if (str[i] == ' ') {
words++;
while (1)
if (str[i] == ' ')
i++;
else {
i--;
break;
}
}
}
words = words + 1;
return (words);
}
float AverageLetters(float words, float letters)
{
float a = (double)(letters / words);
return a;
}
int main()
{
char array[255];
int words = 0;
int letters = 0;
cout << "Enter the string\n\n";
gets_s(array);
int size;
for (size = 0; array[size]; size++)
;
char* str = new char[size];
CountWordsAndLetters(str, words, letters);
cout << "\nAverage number of letters per word: "
<< AverageLetters(words, letters);
return 0;
}
If you know how to make an implementation in one function, please write.
Here, you are allocating an uninitialized array of char:
char* str = new char[size];
You put nothing in it.
You then pass it to CountWordsAndLetters:
// here -------v
CountWordsAndLetters(str, words, letters);
You should consider simply sending array instead:
CountWordsAndLetters(array, words, letters);
Here's a live example of your code working.
Write a program that uses the function isPalindrome given in Example 6-6 (Palindrome). Test your program on the following strings:
madam, abba, 22, 67876, 444244, trymeuemyrt
Modify the function isPalindrome of Example 6-6 so that when determining whether a string is a palindrome, cases are ignored, that is, uppercase and lowercase letters are considered the same.
The isPalindrome function from Example 6-6 has been included below for your convenience.
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length – 1 – i]) {
return false;
} // if
} // for loop
return true;
}// isPalindrome
Your program should print a message indicating if a string is a palindrome:
madam is a palindrome
My program so far is this
#include <iostream>
#include <string.h>
using namespace std;
int main () {
bool isPalindrome (string str);
string str;
int length = str.length();
cout << "Enter a string: ";
getline (cin,str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << "Is not a Palindrome";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
}
I do not know what im doing wrong I sent everything to make sure it matches the word backwards and then when it is true it will return true. I am very to new to programming and I am sorry if my code is a little sloppy.
This is a modification of your code. It wasn't too logical that you were declaring the function inside so i just put it outside.
#include <iostream>
#include <string.h>
using namespace std;
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << "Is not a Palindrome";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
return false;
}
int main () {
string str;
cout << "Enter a string: ";
getline (cin,str);
isPalindrome(str);
}
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000
bool isPalindrome(int x){
int c[MAX];
int i = 0;
int j;
int k = 0;
bool z;
if(x < 0){
return false;
}
while (x != 0){
int r = x % 10;
c[i] = r;
i++;
x = x / 10;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", c[j]);
}
for(k = 0; k <= (i / 2); k++){
if(c[k] == c[i - k - 1]){
z = true;
}
else
{
z = false;
}
}
return z;
}
#include <iostream>
#include <cstring>
#include <string>
//#include <cctype>
using namespace std;
string k;
int key[1024];
int size = 0;
int count = 0;
char text[1024];
char o;
void encrypt(char input[]);
void decrypt(char input[]);
// get arguments from command line
int main(int argc, char *argv[])
{
if(argc >= 3)
{
k = argv[2];
// removing spaces from key
// storing key into char array
for(int i = 0; i < k.length(); i++)
{
if(k.at(i) == ' ')
{
key[i] = k.at(i+1);
i++;
}
else
{
key[i] = k.at(i);
// cout << key[i] << endl;
}
size++;
}
if(strcmp(argv[1], "-e") == 0)
{
// get text from user
// encrypt
cout << "encryption " << endl;
cin.getline(text, sizeof(text));
encrypt(text);
}
else if(strcmp(argv[1], "-d") == 0)
{
// get text from user
// decrypt
cout << "decryption " << endl;
decrypt(text);
}
}
}
void encrypt(char input[])
{
string word = input;
char wkey[word.length()];
// cout << word.length();
for(int i = 0; i < word.length(); count++, i++)
{
// if input is larger than the key
// chars of key will repeat until the size is the length of the input
if(i > size - 1)
{
if(i == size)
{
count = 0;
}
wkey[i] = wkey[count];
}
else
{
wkey[i] = key[i];
}
// if input is a space, cout a space
if(input[i] == ' ')
{
cout << " ";
}
// if input is something other than a letter, then just print it out
else if(input[i] < 65 || input[i] > 122 || input[i] > 90 && input[i] < 97)
{
cout << input[i];
}
else
{
// ignoring case of the letters in the key
// give the letters a range of 0-25
if(wkey[i] >= 65 && wkey[i] <= 90)
{
wkey[i]-= 'A';
}
else if(wkey[i] >= 97 && wkey[i] <= 122)
{
wkey[i]-= 'a';
}
// cout << wkey[i] << endl;
// if input is uppercase, put it in the range of 0-25
// make shift by adding to char in key
// mod by 26 to wrap around
// o is the encrypted character that will be printed
if(input[i] >= 65 && input[i] <= 90)
{
o = ((wkey[i] + (input[i] - 'A')) % 26) + 'A';
}
else if(input[i] >= 97 && input[i] <= 122)
{
o = ((wkey[i] + (input[i] - 'a')) % 26) + 'a';
}
}
cout << o;
}
}
The problem is that I am having trouble encrypting plaintext if that text contains a space. If the text is just one single word, then the program works. In the encryption function where I test the input for a space, I just print out a space, and then the next iteration of the for loop occurs. I think that this problem is occurring because once that next iteration occurs, the character in the key that is at the same index as the space in the input is skipped. I've tried doing an if statement to roll back to the skipped letter in the key, but I still get the wrong output. If anyone could give me some advice on how to fix this problem, I would really appreciate it.
I have this code:
Str UpperCase()
{
Str Result;
int i = 0;
for (; string[i]; i++)
{
if (string[i] <= 'z' && string[i] >= 'a')
{
string[i] -= 32;
}
Result.string[i] = string[i];
}
Result.string[i] = 0;
return Result;
}
It will make String Uppercase.
What should I do if I want it to be Decussate?
Example: hi my name is pooya ==> Hi My NaMe Is PoOyA
Sorry for my bad english
and Thanks ;)
Str UpperCase()
{
Str Result;
int i = 0;
int decussate = 0;
for (; string[i]; i++)
{
if (string[i] <= 'z' && string[i] >= 'a')
{
decussate++;
if( decussate%2 == 1 ){
string[i] -= 32;
}
}
Result.string[i] = string[i];
}
Result.string[i] = 0;
return Result;
}
Add int decussate, by changing it between an odd and an even number everytime a lowercase letter is found, it will create a pattern in which the 1,3,5,7,and so on letters will be capitalized, assuming the string is in lowercase.
Examples:
char test1[] = " ";
char test2[] = " hello z";
char test3[] = "hello world ";
char test4[] = "x y z ";
Results:
" "
" olleh z"
"olleh dlrow "
"x y z "
The problem:
Reverse every world in a string, ignore the spaces.
The following is my code. The basic idea is to scan the string, when
finding a word, then reverse it. The complexity of the algorithm is
O(n), where n is the length of the string.
How do verify it? Is there a better solution?
void reverse_word(char* s, char* e)
{
while (s < e)
{
char tmp = *s;
*s = *e;
*e = tmp;
++s;
--e;
}
}
char* word_start_index(char* p)
{
while ((*p != '\0') && (*p == ' '))
{
++p;
}
if (*p == '\0')
return NULL;
else
return p;
}
char* word_end_index(char* p)
{
while ((*p != '\0') && (*p != ' '))
{
++p;
}
return p-1;
}
void reverse_string(char* s)
{
char* s_w = NULL;
char* e_w = NULL;
char* runner = s;
while (*runner != '\0')
{
char* cur_word_s = word_start_index(runner);
if (cur_word_s == NULL)
break;
char* cur_word_e = word_end_index(cur_word_s);
reverse_word(cur_word_s, cur_word_e);
runner = cur_word_e+1;
}
}
Your code seems correct, but it's plain C. In C++, using the same approach, it could look something like this:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>
int main()
{
std::string str = " cat cow dog wolf lobster";
std::string result;
auto it = str.begin();
while (it != str.end()) {
while (it != str.end() && isspace(*it)) {
result += *it;
++it;
}
auto begin = it;
while (it != str.end() && !isspace(*it)) {
++it;
}
auto end = it;
result.append(std::reverse_iterator<decltype(end)>(end),
std::reverse_iterator<decltype(begin)>(begin));
// if you want to modify original string instead, just do this:
std::reverse(begin, end);
}
std::cout << result <<'\n';
}
In-place, ANSI C89.
#include <ctype.h>
#include <stdio.h>
void reverse(char *s) {
int i = 0, j, k;
while (1) {
while (isspace(s[i])) i++;
if (!s[i]) return;
for (j = i; !isspace(s[j]) && s[j] != '\0'; j++);
for (k = 0; k < (j - i) / 2; k++) {
char t = s[i + k];
s[i + k] = s[j - k - 1];
s[j - k - 1] = t;
}
i = j;
}
}
int main(int argc, char**argv) {
if (argc != 2) return 1;
reverse(argv[1]);
printf("%s\n", argv[1]);
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[89];
cout << "enter\n";
gets(s);
int k;
int p = strlen(s);
strcat(s," ");
for(int i=0; i <= p; i++)
{
if(s[i]==' ')
{
for (k = i-1; (k != -1) && (s[k] != ' '); k--)
cout<<s[k];
cout<<" ";
}
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100],a[10],s[100]=" ";
int i,j;
cout<<"enter a string";
cin.getline(str,100);
strcat(s,str);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' '&&s[i+1]!=' '&&s[i+1]!='\0')
{
cout<<" ";
if(i==0)cout<<"\b";
j=i+1;
while(s[j]!=' '&&s[j]!='\0')
{
j++;
}
j--;
while(s[j]!=' ')
{
cout<<s[j];
j--;
}
}
else if(s[i]==' ')
{
cout<<" ";
if(i==0)cout<<"\b";
}
}
return 0;
}
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
void Reverse_Each(char st[60])
{
int i,j,k=0,p=0,l;
char sr[60];
l=strlen(st);
for(i=0;i<=l;i++) {
if((st[i]==' ')||(st[i]=='\0')) {
for(j=i-1;j>=p;j--) {
sr[k]=st[j]; k++;
}
sr[k]=' '; k++; p=i+1;}
}
for(i=0;i<p;i++)
cout<<sr[i];
}
int main(){
char s[60];
gets(s);
Reverse_Each(s);
return 0;
}