Write a c program to reverse a string






Reverse a string in c without using temp
String reverse using strrev in c programming language

#include<stdio.h>
#include<string.h>
int main(){
    char str[50];
    char *rev;
    printf("Enter any string : ");
    scanf("%s",str);
    rev = strrev(str);
   
    printf("Reverse string is : %s",rev);
   
    return 0;
}

String reverse in c without using strrev
String reverse in c without using string function
How to reverse a string in c without using reverse function

#include<stdio.h>
int main(){
    char str[50];
    char rev[50];
    int i=-1,j=0;

    printf("Enter any string : ");
    scanf("%s",str);
   
    while(str[++i]!='\0');

    while(i>=0)
     rev[j++] = str[--i];

    rev[j]='\0';
  
    printf("Reverse of string is : %s",rev);
  
    return 0;
}

Sample output:
Enter any string : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc

Reverse a string in c using pointers
C program to reverse a string using pointers

#include<stdio.h>
int main(){
    char str[50];
    char rev[50];
    char *sptr = str;
    char *rptr = rev;
    int i=-1;

    printf("Enter any string : ");
    scanf("%s",str);
   
    while(*sptr){
     sptr++;
     i++;
    }

    while(i>=0){
     sptr--;
     *rptr = *sptr;
     rptr++;
     --i;
    }

    *rptr='\0';
  
    printf("Reverse of string is : %s",rev);
  
    return 0;
}


Sample output:
Enter any string : Pointer
Reverse of string is : retnioP







3. Write a c program to delete the all consonants from given string.

69 comments:

Unknown said...

write a program if a customer has an account more than 5 years and is a government servant then loan approve , government servant or not an account more than 5 years loan approve , customer account less than 5 years and not a government servant then loan not approve.

Unknown said...

I am not a pro, just tried about what you asked, here is the code, may this help you.


#include
#include
#include
void main()
{
int atime;
char gsev[10];
clrscr();
printf("Enter the time you are active about the account:");
scanf("%d",&atime);
printf("Government servant? Enter YES or NO:");
scanf("%s",gsev);
if(atime>=5 && strcmp(gsev,"yes")==0)
printf("Loan has approved for you!");

else if(atime<5 && strcmp(gsev,"yes")==0)
printf("Loan hasn't approved.");

else if(atime<5 && strcmp(gsev,"no")==0)
printf("Loan hasn't approved.");

else if(atime>=5 && strcmp(gsev,"no")==0)
printf("Loan hasn't approved.");
getch();
}

Anonymous said...

write a program to print the following pattern using 1 loop(i.e. u can use a loop only once any loop)
2 4 6 8 10
1 3 5 7 9

Unknown said...

Acc to gregarian calander it was monday on 01/01/01. If any year is input through the keyboard write a programme to find outwhat is the day on 1st january of this year

Anonymous said...

useful very useful

Unknown said...

any one can give me an explanation for this string reverse program

Unknown said...

i want C program using string to display
Input:KUMAR
Output: K1U2M3A4R5... anyone plz ...

Unknown said...

Hey Surya Kumar Here is the Solution::
#include
#include

void main(){
char str[50];
int i=0;
clrscr();
printf("\nEnter a string::");
gets(str);
while(str[i]!='\0'){
printf("%c%d",str[i],i+1);
i++;
}
getch();
}

Siddharth M said...

#include
#include
#include
void main()
{
char a[40],b[40];
int i,l;
cout<<"\nenter ur string";
gets(a);
l=strlen(a);
for(i=0;i<l;i++)
{
b[i]=a[l-(i+1)];
}
for(i=0;i<l;i++)
{
cout<<b[i];
}}

Unknown said...

Wap to reverse I Am as I ma...

Anonymous said...

#include
#include
#include

int main()
{
int j,l,i;
char s[10];
printf("\n\nEnter the String to get reversed\n");
gets(s);// taking string in variable s

l=strlen(s);
printf("\n\nreverse string is \n");
for(j=l-1;j>=0;j--)
{
printf("%c",s[j]);
}
getch();
}

Anonymous said...

would you please tell me how to check wheather the string is palindrome or not using for loop

Unknown said...

#include
#include
int main()
{
int a[]={1,2,3,4,5,6},n,i=0,flag,j=0,b[25],p;
n=(sizeof(a)/sizeof(int));

flag=0;
for(i=0;i<=n;i++)
{
if(flag==0)
{
if(a[i]%2==0)
{
printf("%d\n",a[i]);
}
if(i==n)
{
flag=1;
i=0;
}
}
if(i<n)
{
if(flag==1)
{
if(a[i]%2!=0)
{
printf("%d\n",a[i]);
}
}
}
}
}

Unknown said...

Using strrev function string and original string .then compare two function by strcmp and then give solution

Unknown said...

second program has a mistake.. while (i>0) not >= because --i become -1 at the end

Anonymous said...

write a programme in c language:Enter a disordered string
the outpt alphabetic ordered string

Unknown said...

my version:
void reverse(char* s, char* r)
{
r+=strlen(s);
*r--='\0';

while( *s )
*r-- = *s++;
}

Unknown said...
This comment has been removed by the author.
Unknown said...

hay Anjali thanx for submit this. here is solution for u

#include
#include
int main(){
char str[50];
char *rev;
printf("Enter any string : ");
scanf("%s",str);
rev = strrev(str);

printf("Reverse string is : %s",rev);

return 0;
}

Unknown said...

Guys code for
Input: 12/02/1993
Output: 12-feb-1993

CHETAN said...

want prog ;
input : vector 123 india 567
output : vector 321 india 765

Elangovan Manickam said...
This comment has been removed by the author.
Elangovan Manickam said...

#include
int main()
{
int inc=2,i,j=0;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("\n");
j=-1;
}
j+=inc;
printf("%d ",j);
}
}

Elangovan Manickam said...

Try this program. I hope this is the one you want.

#include
int main()
{
int i=0,j=0,spacepos=0;
char temp;
char str[]="I am Elangovan";
printf("%s\n",str);

for(i=0;str[i]!='\0';i++)
{
for(j=i;str[j]!=' '&&str[j]!='\0';j++);
spacepos=j;
j--;
for(;j>i;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
i=spacepos;
}

printf("%s\n",str);
}

Elangovan Manickam said...

@CHETAN, This might help you..

output:
---------
vector 123 india 567
vector 321 india 765


program:
---------------
#include
int main()
{
int i=0,j=0,spacepos=0,isnum;
char temp,ascii;
char str[]="vector 123 india 567";
printf("%s\n",str);

for(i=0;str[i]!='\0';i++)
{
isnum=1;
for(j=i;str[j]!=' '&&str[j]!='\0';j++)
{
ascii = str[j]-48;
if(!(ascii>=0 && ascii<=9))
isnum=0;
}

if(!isnum)
{
i=j;
continue;
}

spacepos=j;
j--;
for(;j>i;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
i=spacepos;
}

printf("%s\n",str);
}


Elangovan Manickam said...

@Amrut

output:
------------
12/02/1993
12-02-1993

program
--------------
#include
int main()
{
int i;
char str[]="12/02/1993";

printf("%s\n",str);
for(i=0;str[i]!='\0';i++)
{
str[i]=(str[i]=='/')?'-':str[i];
}
printf("%s\n",str);
}

Elangovan Manickam said...

Try this program to sort. this is like normal sorting algorithm. you can use any algorithm as you want to use with an integer array.

this program does not sort if capital letters present. In that case you have to convert the value to small letters while comparing. anyway this solves your basic question.

output:
----------
elangovan
aaeglnnov

program
--------------
#include
int main()
{
int i,j;
char temp;
char str[]="elangovan";

printf("%s\n",str);
for(i=0;str[i+1]!='\0';i++)
{
for(j=i+1;str[j]!='\0';j++)
{
if(str[i]>str[j])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
printf("%s\n",str);
}

Unknown said...

Create a 1-D 25-elements character array named myString.
o Ask the user to input a string and store it in the array.
o Pass the string to a function named reverse, which prints the string in reverse order (without
using a temporary variable or using another array).

Unknown said...

write a program that take input a name as array of character through pointer and print the address of each character in memory also print the address of pointer

Unknown said...

Write a C program that take input 5 diferent student from user and save them in file using standard io by arranging them in ascending order by name while each record contain student's name seat no obtained marks and percentage

Unknown said...

CAN ANYONE SOLVE THESE PROGARM.
.
1. Two Character strings may have many common substrings.

For example, photograph and tomography have several common substring of length one (i.e., single letters), and common substring ph, to, and ograph (as well as all the substrings of ograph). The maximum common substring length is 6.

(a) WAP to find the maximum common substring from given two strings and also find maximum common substring length.

2. Let L be an array of n distinct integers.

(a) WAP to find the length of a longest increasing subsequence of entries in L, For example if the entries are 11,17,5,8,6,4,7,12,3, then longest increasing subsequence is 5,6,7,12.

(b) How much time does your algorithm take?

3 Let X = x1x2…xn and Y = y1y2….yn be two character strings. We say that X is a cyclic shift of Y if there is some r such that X = y r+1…yny1…yr.

WAP to determine whether X is cyclic shift of Y or not.

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

1. Two Character strings may have many common substrings.

For example, photograph and tomography have several common substring of length one (i.e., single letters), and common substring ph, to, and ograph (as well as all the substrings of ograph). The maximum common substring length is 6.

(a) WAP to find the maximum common substring from given two strings and also find maximum common substring length.

@ Vikas Saxena..

#include
#include
#include
void main()
{
clrscr();
char a[40],b[40],c[40],d[40]="";
int i,w,x,p,j,k=0;
gets(a);
fflush(stdin);
gets(b); int h=1;i=0,j=0;
while(a[i]!=0)
{
p=i;
j=0;
while(b[j]!=0)
{
if(a[p]==b[j] && h==1)
{
c[k]=a[p];
k++;
p++;
}
else
{
if(a[p]==b[j+1])
{
h=1;
}
else
{
h=0;
}
}
j++;
}
c[k]=0;
w=strlen(d);
x=strlen(c);
if(x>w)
{
strcpy(d,c);
k=0;
}
if(x<w)
{
k=0;
p=i;
}
i++;
}


printf(" %s %d",d,w);
getch();
}

Unknown said...

Write a C main program named as String_Occurences
Use necessary logic to find the occurrences of repeated alphabets in the input string. Note: Assume the input string as “GOOGLE”. anyone please help

vivi said...
This comment has been removed by the author.
Unknown said...

#include
#include
#include
#include
void main()
{
char a[40],b[30],*p,*q,*r;
gets(a); int i=1;
p=a;
q=b;
while(*p!=0)
{
r=p+1;
while(*r!=0)
{
if(*p==*r)
{
i++;
*r=INT_MIN;
printf("%c %d\n",*p,i);
i=1;
}
r++;
}
p++;
}

getch();
}

output: G 2
O 2

Unknown said...
This comment has been removed by the author.
Unknown said...

Q: write a program to print following output from given input
input: aaa bbb ccc ddd eee fff ggg hhh iii jjj
output: bbb aaa ddd ccc fff eee hhh ggg jjj iii..Plz give me answer of this

sirishanth said...

write a programme using pointers,
input is string,
if i select position in a string
left side of the string is same
right side of the string will be reverse
any body help me

Unknown said...

#include
#include
#include
void main()
{
clrscr();
char a[50],b[50],*p,*q,*r,*s;
gets(a);
int i=0;
strcat(a," ");
p=a;
s=b;
q=a;
while(*p!=0)
{
if(*(p+1)==' ')
{
if(i==0)
{
i=1;
p++;
}
else
{
r=p+2;
while(q!=p)
{
*s=*p;
s++;
//printf("%c",*p);
p--;
}
*s=*p;
s++;
//printf("%c ",*p);
q=r;
p=r;
*s=' ';
s++;
i=0;
}
}
else
{
p++;
}
}
s--;
*s=0;
strcpy(a,b);
printf("%s",a);
getch();
}

input: aaa bbb ccc ddd eee fff ggg hhh iii jjj
output: bbb aaa ddd ccc fff eee hhh ggg jjj iii

Unknown said...

#include
#include
#include
void main()
{
clrscr();
char a[50],*p,*q,x;
gets(a);
int i=0,pos,length;
fflush(stdin);
printf("Enter the position\n");
scanf("%d",&pos);
p=a;
length=strlen(a);
q=p+(length-1);
while(*p!=0)
{
if(pos==i)
{
length=length-pos;
pos=length/2;
while(length!=pos)
{
x=*p;
*p=*q;
*q=x;
p++;
q--;
pos++;
}
break;
}
else
{
p++;
i++;
}
}
printf("%s",a);
getch();
}

input is: tishuabcdef
enter position: 4
output: tishfedcbau

sirishanth said...

tq tishu singh, nyc code

sirishanth said...

write a programme using pointers,
input is string,
i select position in a string
if position place is even the even places of characters will delete and print
same for odd also
any body help me

uni_detector said...

This is an Assignment Question from morpheus TMF1414 Sem1 2015/2016

Unknown said...

678
123
049
write a c program , which will give o/p as 678394012
and generalize to for higher order matrix.

Unknown said...

string s1=satya sting s2=tech
output is hscaettya
first letter string s2 reverse letter next sting s1 first letter
i want program

Unknown said...

how to print reverse of string like example
i/p- welcome to vector
o/p- vector to welcome

venu said...

why did u used int i?

venu said...

why did u used int i?

Unknown said...

how to reverse a string in python

Unknown said...

Assignment2
Write c program that reads 20 students.
1. New students
2. intermediate students
3. Senior student
If the number is not a 1 or a 2, we assume that it is a 3
If senior student>10 print
“High score”

Unknown said...

how to create a c program of mirror number 5? please help me. not reverse .but mirror.

shf said...

given a string char str[]="123456789";
write a program that display the following:
1
232
34543
4567654
567898765

Unknown said...

Write a program that reads a string from user input and reverses the letter case of all characters in the string. Ignore numeric and special characters.

Unknown said...

Help me to solve this.....

Declare and write a function that changes the value of the passed in argument, so that the caller of the function gets the changes from outside.

Unknown said...

Help me to solve this.....

Declare and write a function that changes the value of the passed in argument, so that the caller of the function gets the changes from outside.

Unknown said...

Give me ans.

What’s the name of the header file to include for using C++ style string in your program?

someone said...

plzz tell me c program to print ur address

Unknown said...

Anyone help me!!!Given a list of space separated words, reverse the order of the words.
Input :
this is a test
foobar
all your base

Output:
Case #1: test a is this
Case #2: foobar
Case #3: base your all

bhuvi said...

can anyone tell me hw to reverse a string and hw it work by using for loop

Tayaba said...

any one give me program of this statement ...
" suppose you have a main with three local arrays all the same size and type float the 1st two are initialize to value write a function called addarrays() that accept the addresses of three array as pointer location adds the constants 1st two array together element by element and places the result in third array and print third array before returning .."
pease mail me sson .. as early as possible ....

Unknown said...

anyone please suggest me string reverse pgm
Input String:have a nice day
Output string:evha a ecni yad

Trending Apps said...

#include
#include
void main()
{
char str[50];
int i;
printf("Enter any string=);
gets(str);
for(i=0;str[i]!=NULL; i++);
for(i=i-1; i>=0; i--)
{
printf("%c",str[i]);
}
getch();
}

Unknown said...

Please tell in pattern programming
This is output
4
33
222
1111
I am sea

Sarthak said...

WAP to reverse a line as the following pattern :
Original : “LPU is located in punjab”
Reverse :” UPL si detacol ni bajnup”

Unknown said...

#include
#include//WAP to check and print Armstrong Nos. between 1 to 500
int main()
{int i=1,t,temp,r=0,c=0,sum=0;
printf("Armstrong Nos. between 1 to 500 are:\n");
while(i<500)
{t=i;
while(t!=0)
{t=t/10;
c++;
}
temp=i;
while(temp!=0)
{r=temp%10;
sum=sum+pow(r,c);
temp=temp/10;
}
if(sum==i)
printf("%d",sum);
i++;
}
}
Can someone tell what is wrong with my code as I am getting only 1 as output.