In: Computer Science
Are my codes correct?
#include <iostream>
#include <string>
#include <cassert>
#include <cctype>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include<fstream>
int locateMinimum( const std::string array[ ], int n ){
if(n <= 0){
return -1;
}
else{
int minInd = 0;
for(int i = 0; i < n; ++i){
if(array[i] < array[minInd]){
minInd = i;
}
}
return minInd;
}
}
int countPunctuation( const std::string array[], int n)
{
int count = 0;
for (int i=0;i<= n;i++)
{
if (array[i]== ('!', ',' ,':' ,';' ,'.', '-' ,'?'))
{
count = count + 1;
return count;
}
else {
return 0;
}
bool hasReverse(const std::string array[ ], int n )
{
// entire array
for(int i=0;i<n;i++)
{
//finding the reverse of the string
string rev = string(array[i].rbegin(),array[i].rend());
//checking that reverse of the string is present in the array or not
//if present returning true or we are returning false
for(int j=0;j<n;j++)
{
if(array[j] == rev){
return true;
}
}
}
return false;
}
char highestOccurredCharacter(const std::string array[ ], int n, int index ){
if( n <= 0 || index >= n || index < 0) //if invalid array length n or invalid index is given the function returns null character
return '\0';
int highest=0; //initially considering the highest to be 0
char c = '\0'; //to save the most repeating character
for(int i=0;i<array[index].length();i++){ //traversing through the string array[index]
int count = 0; //initial count of character array[index][i] is 0
for(int j=0;j<array[index].length();j++) //traversing through the string array[index] again to find the count of repeatetions of array[index][i] character
if(array[index][i]==array[index][j])
count++;
if(count>highest){ //if count is greater than the highest count then we have a new highest which has to be stored in highest
highest = count; //save the latest highest in highest
c = array[index][i]; //save the character of latest highest in c
}
}
return c; //return the total highest repeated character
}
bool isInIncreasingOrder( const std::string array[ ], int n )
{
if(n < 0){
return false;
}
else if(n == 0){
return true;
}
else{
for(int i = 1; i < n; ++i){
if(fruit[i] <= fruit[i - 1]){
return false;
}
}
return true;
}
}
char firstNonRepeatedCharacter(const std::string array[ ], int n, int index);
//This function finds out the first non repeating chracter and returns it
if( n <= 0 || index>= n || index < 0) { //if invalid array length n or invalid index is given the function returns null character
return '\0';}
for(int i=0;i<array[index].length();i++){ //traversing through the string array[index] chracters
int count = 0; //initialising count to 0 initially
for(int j=0;j<array[index].length();j++) //traversing through the string array[index] chracters to count the occurrences
if(array[index][i]==array[index][j]) //
count++;
if(count==1) //the count of non Repeating Character is 1 so if any chracter gets a count 1 it must be returned and it would be the firstNonRepeatedCharacter
return array[index][i];
}
bool isOnlyDigits( const std::string array[ ], int n ){
//traversing array
for(int i=0;i<n;i++)
{
//traversing each string of the array
for(int j=0;j<array[i].length();j++)
{
//checking the string contains only digits or not if not we are returning false
if (!(array[i][j] >= '0' && array[i][j] <= '9'))
return false;
}
}
return true;
}
}
int countPunctuation(const std::string array[], int n) {
int count = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < array[i].length(); j++) {
if(array[i][j] == '!' || array[i][j] == ',' || array[i][j] == ':' || array[i][j] == ';' || array[i][j] == '-' || array[i][j] == '?')
count++;
}
}
return count;
}
bool hasReverse(const std::string array[], int n) {
for(int i = 0; i < n; i++) {
string rev = string(array[i].rbegin(),array[i].rend());
if(array[i] != rev)
return false;
}
return true;
}