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.

56 comments:

  1. 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.

    ReplyDelete
    Replies
    1. #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();
      }

      Delete
    2. why did u used int i?

      Delete
    3. why did u used int i?

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

      Delete
  2. 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();
    }

    ReplyDelete
  3. 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

    ReplyDelete
    Replies
    1. #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]);
      }
      }
      }
      }
      }

      Delete
    2. This comment has been removed by the author.

      Delete
    3. #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);
      }
      }

      Delete
  4. 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

    ReplyDelete
  5. useful very useful

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

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

    ReplyDelete
  8. 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();
    }

    ReplyDelete
  9. #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];
    }}

    ReplyDelete
  10. Wap to reverse I Am as I ma...

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. 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;
      }

      Delete
    3. 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);
      }

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

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

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

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

    ReplyDelete
    Replies
    1. 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);
      }

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

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

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

    ReplyDelete
    Replies
    1. @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);
      }

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

    ReplyDelete
    Replies
    1. @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);
      }


      Delete
  18. 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).

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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

    ReplyDelete
  21. 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.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. 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();
      }

      Delete
  22. 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

    ReplyDelete
    Replies
    1. #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

      Delete
    2. This comment has been removed by the author.

      Delete
  23. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This is an Assignment Question from morpheus TMF1414 Sem1 2015/2016

      Delete
  24. 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

    ReplyDelete
    Replies
    1. #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

      Delete
  25. 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

    ReplyDelete
    Replies
    1. #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

      Delete
    2. tq tishu singh, nyc code

      Delete
  26. 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

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

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

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

    ReplyDelete
  30. how to reverse a string in python

    ReplyDelete
  31. 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”

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

    ReplyDelete