WRITE C++ CCODE FOR THE FOLLOWING QUESTION USING CHAR AND C STRING You are requi
ID: 3701642 • Letter: W
Question
WRITE C++ CCODE FOR THE FOLLOWING QUESTION USING CHAR AND C STRING
You are required to create a character array named “mywords” of size 10. Initialize it with random characters taken from user. Write C++ code that will reverse the order of the characters in this array.
(You can use only 1 extra variable for this program, No extra array or more than 1 variable is allowed to be used).
Write a function that takes in two arrays of type char and length n, m and shuffles them in a way that their alternate characters are swapped. If one array is shorter than the other, then the longer array will continue with its own elements. For example
Array 1 = f g h u k m v
Array 2 = u h b d a s k
Output
Array 1 = u g b u a m k
Array 2 = f h h d k s
Write a function to find out how many digits are in a string. Your function should take one parameter: the string to search through. Your function should return a single integer: the number of digits contained in that string. Your function must have this prototype:
int countDigits(string);
For example, if the string was:
"Password123!"
Your function should return 3, as the string contains 3 digits: ‘1’, ‘2’, and ‘3’.
Explanation / Answer
#include<iostream>
using namespace std;
//method to reverse the order the elements in char array...
//using only one variable
void reverse(char a[],int n)
{
int i=0;
n--;
while(i<n)//reversing array
{
char k=a[i];
a[i]=a[n];
a[n]=k;
i++;
n--;
}
}
void print_array(char a[],int n)
{
int i=0;
while(i<n)
{
cout<<a[i]<<" ";
i++;
}
cout<<" ";
}
//method to shuffle the two given arrays
void shuffle(char a[],int n,char b[],int m)
{
if(n>m)
n=m;
int i=0;
while(i<n)
{
if(i%2==0)//swapping at every even position
{
char k =a[i];
a[i]=b[i];
b[i]=k;
}
i++;
}
}
//method to find the length of the array
int find_len(char a[])
{
int i=0;
while(a[i]!='')i++;
return i;
}
//method to find the number of digits in given string
int countDigits(string s)
{
int i=0;
int count=0;//to count the number of digits
while(s[i]!='')//end of the string...
{
if((int)s[i]>=48 && (int)s[i]<= 57)
{
count++;
}
i++;
}
return count;
}
//testing method
int main()
{
char a[10];
cout<<"Enter a random string:";
cin>>a;
int n = find_len(a);
reverse(a,n);//method to reverse
cout<<"Reversed string is:";print_array(a,n);
char aa[20],bb[20];
cout<<"Enter array 1:";
cin>>aa;
cout<<"Enter array 2:";
cin>>bb;
int x= find_len(aa),y=find_len(bb);
shuffle(aa,x,bb,y);
cout<<"Arrays after shuffling: ";
cout<<"Array 1:";
print_array(aa,x);
cout<<"Array 2:";
print_array(bb,y);
string s;
cout<<"Enter string to count digits:";
cin>>s;
int c=countDigits(s);
cout<<"The number of digits in string :"<<c<<endl;
return 0;
}
output:
Enter a random string:abcd
Reversed string is:d c b a
Enter array 1:fghukmv
Enter array 2:uhbdas
Arrays after shuffling:
Array 1:u g b u a m v
Array 2:f h h d k s
Enter string to count digits:Password123!
The number of digits in string :3
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.