Computer Science, asked by tanvi414, 1 year ago

plz help......write a C++program to reverse all strings stored in two dimensional array of characters...

Answers

Answered by MrPerfect0007
4
String is a sequence of characters. char data type is used to represent one single character in C++. So if you want to use a string in your program then you can use an array of characters.

The declaration and definition of the string using an array of chars is similar to declaration and definition of an array of any other data type.

Any string ends with a terminating null character ‘\0’. An array definition in such a way should include null character ‘\0’ as the last element.

C++ provides following two types of string representations:

The C-style character string.
The string class type introduced with Standard C++.
The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character ‘\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.

Here is source code of the C++ Program to Reverse an Array of Strings. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

SOURCE CODE : :

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* C++ Program to Reverse an Array of Strings */

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main()
{
char a[3][50];
int i,j,k,len;
cout<<"\nEnter 3 strings: \n";

for(i=0;i<3;i++)
{
cout<<"\nEnter [ "<<i+1<<" ] String :: ";
gets(a[i]);
}

cout<<"\nThe Array of Original Strings :: \n";

for(i=0;i<3;i++)
{
cout<<"\n"<<a[i]<<" \n";
}

cout<<"\nThe Array of Reversed Strings :: \n";

for(i=0;i<3;i++)
{
cout<<"\n";
len=strlen(a[i]);
for(j=0,k=len-1;k>=0;j++,k--)
{
cout<<a[i][k];
}
cout<<" \n";
}

return 0;
}

tanvi414: thnx
Answered by Ruhanika105
3
Hello there!!!!!!


#include≤iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
const int SIZE = 80;
void main( )
{
char a[10][SIZE],temp;
int n,I,j,mid, length;
clrscr( );
cout<<"Enter the number of strings in the array<=10";
cin>>n;
cout<<"Enter"<<n<<"strings";
for(i=0; i<n; i++)
gets(a[i]);
clrscr( );
cout<<"inputted strings are";
for(i= 0;I<n;i++)
{
length= strlen(a[i]);
mid = length/2;
for(j= 0;j<mid;j++);
{
temp = a[i][j];
a[i][j] = a[i][length-j-1];
a[i][length-j-1]= temp;
}
}
cout<<"Reversed strings are";
for(i=0;i<n;i++)
puts(a[i]);
getch( );
}


hope it helps!!!!

tanvi414: thank u soo much......
tanvi414: it was very helpful
Ruhanika105: my pleasure :)
Similar questions