Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello i want Reverse string in c just using (string.h :(strcat and strcpy and strcmp))
without (poniter and strrev )
sorry for my english
I want these non- answer
C++
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void reverse (int index, char *str ) ;
int main (void)
{
   char name[100];
   printf ("Enter a mutli-word string") ;
   gets(name) ;
   reverse (strlen(name) , name ) ;
}
void reverse (int index, char *str )
{
  if (--index < 0 )
  {
     return ;
  }
  else
  {
        putchar ( *(str + index) ) ;
       reverse (index, str) ;
  }
}
Posted
Updated 17-Nov-14 7:42am
v5
Comments
Sergey Alexandrovich Kryukov 16-Nov-14 11:44am    
Maybe, "reverse", or something else? Without pointer and array?! Your teacher is probably too clever. :-)
—SA
OriginalGriff 16-Nov-14 11:51am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

It is not clear what do you mean by "reserve". It's possible that this is a mistake/typo.

However, doing anything to a string "without pointer and array" just sounds absurd. Why? Simply because a C string itself is a pointer; and an array at the same time.

Perhaps you just did not formulate the problem correctly. There is a popular problem often offered during different exams and interviews: given a C string, modify it to rearrange its characters in reverse order, without using another area of memory to store the string. That's it, it should be enough to have one-two auto variables representing intermediate characters or pointers, using the same memory area as the one of the source string. The problem is quite solvable, but with different degree of elegance. If that is a problem are interested in, we can discuss it, but no sooner than you show what have you tried so far.

—SA
 
Share this answer
 
v2
Comments
CPallini 16-Nov-14 11:59am    
5. I suppose you are right, OP has to reverse a C string 'in-place'.
Sergey Alexandrovich Kryukov 16-Nov-14 12:34pm    
Thank you, Carlo.
And even if OP hasn't, the answer is given: no activity without pointer/array is possible here. :-)
—SA
Is this fulfilling your constraints?
C
size_t pos = strlen(name);
while(pos > 0) putchar(name[--pos]);
Cheers
Andi
 
Share this answer
 
Newer solution

I think this is closer to your requirements?

C#
void reverse (int len, char *str )
{
    char *tmp=(char*)calloc(len, sizeof(char));
    const char *p=0;
    while(len--)
    {
        p=(str+len);
        strncat(tmp, p, 1);
    }

    strcpy(str,tmp);
    free(tmp);
}


Previous solution

I tried to do without pointers, but this best I work out.

C#
void reverse (int len, char *str )
{
    char rev[100];
    memset(rev,0,100*sizeof(char));
    int i=len;
    const char* p;
    while(i)
    {
        p=str+i-1;
        strcat(rev, p);
        *(str+i-1)='\0';
        i--;
    }
    strcpy(str,rev);
}
 
Share this answer
 
v2
Comments
Mortadda jafar 17-Nov-14 5:07am    
hay
i want reserve without chang index for array
exmple with change index:
<pre lang="c++"></pre>
char array[20];
int i,j;
gets(array);
j=strlen(array);
for(i=j-1;i>=0;i--)
{printf("%c",array[i])
}


i want use (strncat & strncpy with for)...for Reverse string
but i don't know how i can do this
::
ex ::
<pre lang="c++"></pre>
char array[20];
int i,j;
j=strlen(array);
for(*******)
{
strncat(****) or strcat(***)
or strncpy(***) or strcpy;
}


and in the end i want Reverse string :^) :^)
C++
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    char p[20], t;
    int i, n, j;
    gets(p);
    n = strlen(p) - 1;
    for (i = n, j = 0; i >= n / 2; i--, j++) {
        strncpy(&t, p + j, 1);
        strncpy(p + j, p + i, 1);
        strncpy(p + i, &t, 1);
    }
    puts(p);
}
 
Share this answer
 
v2
Comments
Andreas Gieriet 18-Nov-14 18:58pm    
This has not even didactical sense. Why the heck do you employ strncpy or alike for placing *one* character at one position in a buffer? This is crap, sorry.
Think about the economics of programming - this is overkill and not maintainable, not easy readable, error prone. I can't see any positive aspect in such a solution.
Andi
PS: It's also "uncommon" to provide your own solution as a "solution". If you found an answer yourself, you may improve your question and tell so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900