about the function strcpy_s - c++

I want to output the smallest letters of the first letter of three countries, this is the code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
void smallest_string(char str[][30], int);
int i;
char country_name[3][30];
for (i = 0; i < 3; i++)
cin >> country_name[i];
smallest_string(country_name, 3);
system("pause");
}
void smallest_string(char str[][30], int n)
{
int i;
char string[30];
***strcpy_s(string, str[0]);***
for (i = 0; i < n; i++)
if (strcmp(str[i], string) < 0)
strcpy_s(string, str[i]);
cout << endl << "the smallest string is:" << string << endl;
}
In this code strcpy_s(string, str[0]);, it seems can be deleted. Why?

The line strcpy_s(string, str[0]); is essential if you want your loop to start from i = 1.
However, since you start it from i = 0 it is not strictly required if you also define the following start condition: string[0] == CHAR_MAX and string[1] == 0. But if that is not the case then you will experience undefined behavior.

Related

Is there a way I can fix this issue with length counter function?

I have to find the length of an array of characters using a function that uses the pointer notation and, for some reason, I get 23 but the result should be 5.
Here is the code I built:
#include <iostream>
using namespace std;
int length(char *N)
{
int length = 0;
char* S = N;
for (; *S != '\0'; S++);
for (; *N != *S; N++)
{
length++;
}
return length;
}
int main()
{
char A[5];
char *N;
N = A;
cout << "please enter the charecters you desire" << endl;
for (int i = 0; i <=4; i++)
{
cin >> A[i];
}
cout<<length(N);
}
You don't need two loops.
And note that you have to leave a space in the array (the last element) to fill it by '\0'.
Hence by the two notes and if you want to use five characters excluding the '\0', your code will be as follows
#include <iostream>
size_t length(char *N){
size_t length = 0;
for (; *N != '\0'; N++){
length++;
}
return length;
}
int main()
{
char A[6];
std::cout << "please enter the characters you desire\n";
for (int i = 0; i < 5; i++){
std::cin >> A[i];
}
A[5] = '\0';
std::cout << length(A);
}

how to create an array of Bitset in c++

I want to create an array of Bitset .Binary Bitset(example "100","1010",etc)
After that I want to input from user and store in the the Bitset .
I have tried the following line but it says error.
#include<bits/stdc++>
using namespace std;
int main()
{
int n,i;
string bit_string;
cin>>n // size of Bitset array.
bitset<8> brr[n];//
for(i=0;i<n;i++)
{
cin>>bit_string;
brr[i](bit_string);
}
return 0;
}
I want to create n Bitset each of size 8 bits.Where n is given by user.
my input is binary string like.
"110010","001110"
please help
The error ocurrs because you are trying to creat a C-style array using n which is not compile-time constant. It's not possible to creat a C-style array without being n known at compile time.
The following is a good way to do what you want
Creat a std::vector<std::bitset<8>> to hold your bitset<8>s, as follows.
Note that the code ignores the excess of characters in strings iput like "111111110" (makes it "11111111") and treats any character except '1' as if it were '0' and if the input string is less than 8 characters, the code adds zeros by the default of the bitsets
#include <vector>
#include <bitset>
#include <iostream>
int main() {
int n, i;
std::string bit_string;
std::cout << "Enter the size";
std::cin >> n; // size of Bitset array.
std::vector<std::bitset<8>> brr(n);//
for (i = 0; i < n; i++) {
std::cin >> bit_string;
for (int j{}; j < bit_string.size() && j < 8; ++j) {
brr[i][j] = (bit_string[j] == '1') ? 1 : 0;
}
}
//To test
for(auto const& el :brr)
{
for(int i{}; i < 8;)
std::cout << el[i++];
std::cout<<"\n";
}
}
See Why is "using namespace std;" considered bad practice?
and
Why should I not #include <bits/stdc++.h>?
For dynamic count of the objects , Please try vector<> instead of array[]
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
vector<bitset<8>> arr; //size()=>0
arr.resize(n); //size()=>n
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
return 0;
}
If you still want to use array , please try this way
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
bitset<8>* arr = new bitset<8>[n];
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
delete[] arr; //IMPROTAND , delete the array and free memory
return 0;
}

Trying to make a program that reverses a statement and makes all upper case letters lowercase but it only reverses

The code reverses the statement but it doesn't make the uppercase letters lowercase. I'm pretty new to coding and would appreciate some help, thanks! :
#include<iostream>
#include<cstring>
using namespace std;
int main ()
{
char str[50], temp;
int i, j, x;
cout << "Enter a string : ";
gets(str);
j = strlen(str) - 1;
int size = strlen(str)-1;
for (x=0;x<=size;x++){
if (str[x]>'z'){
str[x]+'32';
}
}
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nReverse string : " << str;
return 0;
}

copying one element of a char array to another using for loop

I'm trying to write a simple program which takes an array of chars, and spits it out backwards. I know there are plenty of other ways to shorten this using a library header function, but I wanted to do it using for loops just to get used to them.
#include<stdio.h>
#include<iostream>
using namespace std;
char string1[10];
int count = 0;
char stringy[10];
void enterString()
{
cout << "please enter a string: " << endl;
cin >> string1;
}
void stringCounter(const char stringLength[])
{
//initiate for loop i = 0
//if stringLength[i] does not does not equal 'i' then carry on
//increment i
for (int i = 0; stringLength[i] != '\0'; i++)
{
count++;
}
cout << "size of string is: " << count << endl;
}
void reverseString(int arraySize, char string2[])
{
int counter = 0;
for (int i = arraySize; i >= 0; string2[i--])
{
stringy[counter] = string2[i];
counter++;
}
stringy[count] = '\0';
cout << stringy << endl;
}
int main()
{
enterString();
stringCounter(string1);
reverseString(count, string1);
return 0;
}
This is the whole program. The program is failing in function reverseString. I can't work out how to successfully read the last index of the char array string2[] and copy it into the first index of char array stringy.
One, If the user enters a string more than 10 characters long then your enterString() function will access the array out of its bound, at cin>>string1. So better to use getline to make sure you don't read more than what your array can hold.
Two, with your current implementation the reverseString() function will write to the first element of the array with the null terminator character,if the arraySize<=10, and trying to display that string will not show you anything.
This:
cin >> string1;//will try to access the array out of its bound if user give more than it can hold,i.e 10 characters
...
for (int i = arraySize; i >= 0; string2[i--])
{
stringy[counter] = string2[i];//the first iteration will put the '\0' character as the first elements of stringy
counter++;
}
Should be changed to:
cin.getline(string1,10);//make sure to get not more than 10 characters,including the null terminator
.....
for (int i = arraySize-1; i >= 0; i--)
{
stringy[counter] = string2[i];
counter++;
}
There are many mistakes in your program. If this is the exact code you are compiling then it should throw many errors.
Following might help.
#include<iostream>
using namespace std;
void reverseString(int , char *);
int stringCounter(const char );
int stringCounter(const char stringLength[])
{
int count = 0;
for (int i = 0; stringLength[i] != '\0'; i++)
count++;
cout << "size of string is: " << count << endl;
return count;
}
void reverseString(int arraySize, char string2[])
{
int counter = 0;
char stringy[100];
for (int i = arraySize - 1; i >= 0; i--)
{
stringy[counter] = string2[i];
counter++;
}
stringy[counter] = '\0';
cout << stringy << endl;
}
int main()
{
char str[] = "string";
reverseString(stringCounter(str),str);
return 0;
}

How the assigned value to variable gets changed automatically?

When I am providing input for std::cin >> diff; it takes the input value, and the moment I am entering the value of array, the diff variables value gets changed and sets the value of 4th element of the array.
Please help me where is it going wrong. I have tried with fflush(std). But it did not help me.
I am using Visual Studio 2010 Ultimate edition.
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i, num;//[]={0};
int diff = 0;
int numset[] = {0};
int temp, cnt;
cnt = num = i = 0;
std::cout << "Enter your number and difference : ";
//fflush(stdin);
std::cin >> num ;
std::cin >> diff;
cout << "Enter array Elements : \n";
for(i = 0; i < num; i++)
{
cin >> numset[i];
//fflush(stdin);
}
for(i = 0; i < num; i++)
{
for(int j = i; j < num; j++)
{
if(i == j)
{
temp = numset[j];
}
else
{
if((diff == (numset[j] - temp)) || (((-1)*diff) == (numset[j] - temp)))
{
cnt++;
}
}
}
}
cout << cnt << endl;
system("pause");
return 0;
}
You're accessing beyond the bounds of the array numset, so your code has Undefined Behaviour (UB) and anything could happen. It could overwrite variables on stack (as it does in your case), it could crash, it could order pizza online.
numset is declared as a single-element array, so accessing numset[i] for i > 0 results in UB. You should probably change numset to be a std::vector<int> and use push_back() to append numbers to it.