String reverse using pointers - c

I'm trying to reverse a string using pointers.When i try to print the reversed string instead of getting DCBA i'm getting out only as BA?Can anyone help me on this?
#include<stdio.h>
void reverse(char *);
void main()
{
char str[5] = "ABCD";
reverse(str);
}
void reverse(char *str)
{
char *rev_str = str;
char temp;
while(*str)
str++;
--str;
while(rev_str < str)
{
temp = *rev_str;
*rev_str = *str;
*str = temp;
rev_str++;
str--;
}
printf("reversed string is %s",str);
}

You're losing your pointer to the beginning of the string, so when you print it out you're not starting from the first character, because str no longer points to the first character. Just put in a placeholder variable to keep a pointer to the beginning of the string.
void reverse(char *str)
{
char *begin = str; /* Keeps a pointer to the beginning of str */
char *rev_str = str;
char temp;
while(*str)
str++;
--str;
while(rev_str < str)
{
temp = *rev_str;
*rev_str = *str;
*str = temp;
rev_str++;
str--;
}
printf("reversed string is %s\n", begin);
}

char* strrev(chr* src)
{
char* dest
int len=0 ; //calculate length of the src string
int index=0 ; // index for dest (output) string
int rindex=0; //Index to be counted from end toward beginning of src string
//Calculate length of the string
//Keep iterating till it reaches to null char '\0'
while(*(src+len) != '\0')
{ len++ }
//pointing rindex at the last character of src string
rindex=len-1;
//Start copying from last char of src string at first index of dest array
// Copying second last char of src string at second index of dest array and so on ..
while(rindex > =0)
{
*(dest+index) = *(src + rindex)
index++;
rindex--;
}
// Finally covert array of char into string , by inserting a null char at the end of dest array
*(dest+index) = '\0';
return dest;
}

Related

trying copy character from pointer to string [duplicate]

This question already has an answer here:
String literals: pointer vs. char array
(1 answer)
Closed 2 months ago.
I am trying to reverse string using pointer (ref source). in function
string_reverse
Bus Error happened at this line when copying character from char pointer end to start char pointer :
*start = *end;
I tried LLDB in VS code. .
Can some one explain why there is bus error happened at below line?
*start = *end
Full code below:
#include <stdio.h>
#include <string.h>
void string_reverse(char* str)
{
int len = strlen(str);
char temp;
char *end = str;
char *start = str;
/* Move end pointer to the last character */
for (int j = 0; j < len-1; j++)
{
end++;
}
for(int i = 0; i< len/2;i++ )
{
temp = *start;
*start = *end;
*end = temp;
/* update pointer positions */
start++;
end--;
}
}
int main( void )
{
char *str = "test string";
printf("Original string is %s\n", str);
string_reverse(str);
printf("Reverse string is %s\n", str);
return 0;
}
Actual result: Bus Error happened at line
*start = *end;
Expected output:
Reverse string is gnirts tset
The error happens because
char * str = "...";
mean you have a pointer to a string literal. It's a constant string that you can not modify.
When you change it to
char str[] = "...";
as chux-reinstate-monica mentioned, str will be char array with the length of your string, and you can modify it without any error.

Why does this not concatenate two strings?

This is the requirement for my code:
This function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.
Returns a pointer to the resulting string dest.
This is the output I am getting:
Hello
World!
Hello World!
Hello
Here is my code:
char *_strcat(char *dest, char *src) {
int lengthd = 0;
int lengths = 0;
int i = 0;
int j = 0;
int k = 0;
char tmp[10];
while (dest[i] != '\0') {
lengthd++;
i++;
}
while (src[k] != '\0') {
tmp[lengths] = src[k];
lengths++;
k++;
}
for (; j < lengths - 1; j++) {
dest[lengthd + 1] = tmp[j];
}
dest[lengthd + 1] = '\0';
return (dest);
}
int main(void) {
char s1[98] = "Hello ";
char s2[] = "World!\n";
char *ptr;
printf("%s\\n", s1);
printf("%s", s2);
ptr = _strcat(s1, s2);
printf("%s", s1);
printf("%s", s2);
printf("%s", ptr);
return (0);
}
Your code fails for multiple reasons:
you use a temporary array to make a copy of the source string: this array tmp has a fixed length of 10 bytes, which is too small if the source string is longer than 10 bytes. Otherwise you will have undefined behavior when you write beyond the end of this array.
there is really no need for this temporary array anyway.
the final loop stops at lengths - 1, hence you stop before the last byte of the copy.
you copy all bytes to the same position dest[lengthd + 1].
you finally set the null terminator at the same position again.
you never changed the null terminator at dest[lengthd] so the function appears to have no effect on dest.
the tests in main() cannot produce the output you posted, probably because of a typo in "%s\\n".
avoid using identifiers starting with an _.
Here is a modified version:
#include <stdio.h>
#include <string.h>
char *my_strcat(char *dest, char *src) {
int i = 0;
int k = 0;
/* find the offset of the null terminator in dest */
while (dest[i] != '\0') {
i++;
}
/* copy the bytes from the src string there */
while (src[k] != '\0') {
dest[i] = src[k];
i++;
k++;
}
/* set the null terminator */
dest[i] = '\0';
/* return the pointer to the destination array */
return dest;
}
int main(void) {
char s1[98] = "Hello ";
char s2[] = "World!";
char *ptr;
printf("%s\n", s1);
printf("%s", s2);
ptr = my_strcat(s1, s2);
printf("%s", s1);
printf("%s", s2);
printf("%s", ptr);
return 0;
}
Note that the source string is not modified and the offsets should have type size_t and can be incremented as a side effect of the assignment:
char *my_strcat(char *dest, const char *src) {
size_t i = 0;
size_t k = 0;
/* find the offset of the null terminator in dest */
while (dest[i] != '\0') {
i++;
}
/* copy the bytes from the src string there */
while (src[k] != '\0') {
dest[i++] = src[k++];
}
/* set the null terminator */
dest[i] = '\0';
/* return the pointer to the destination array */
return dest;
}
You can also use pointers instead of offsets:
char *my_strcat(char *dest, const char *src) {
/* use a working pointer to preserve dest for the return value */
char *p = dest;
/* find the offset of the null terminator in dest */
while (*p != '\0') {
p++;
}
/* copy the bytes from the src string there */
while (*src != '\0') {
*p++ = *src++;
}
/* set the null terminator */
*p = '\0';
/* return the pointer to the destination array */
return dest;
}
One final change: you can combine reading the source byte, copying to the destination and testing for the null terminator, which will have been copied already:
char *my_strcat(char *dest, const char *src) {
/* use a working pointer to preserve dest for the return value */
char *p = dest;
/* find the offset of the null terminator in dest */
while (*p != '\0') {
p++;
}
/* copy the bytes from the src string there */
while ((p++ = *src++) != '\0') {
/* nothing */
}
/* the null terminator was copied from the source string */
/* return the pointer to the destination array */
return dest;
}
At least due to the declaration of the character array tmp with the magic number 10
char tmp[10];
the function does not make a sense.
Moreover in this while loop
while (src[k] != '\0')
{
lengths++;
k++;
tmp[lengths] = src[k];
}
the first element of the array tmp is skipped.
Also in this for loop
for (; j < lengths-1; j++)
{
dest[lengthd + 1] = tmp[j];
}
the condition of the loop is incorrect. Also the expression dest[lengthd + 1] skips the terminating zero character of the string pointed to by the pointer dest. And all characters are written at the same position lengthd + 1.
Apart from this the names s1, s2 and ptr used in main were not declared.
The function can be declared and defined the following way
char * my_strcat( char *dest, const char *src )
{
char *p = dest;
while ( *p ) ++p;
while ( ( *p++ = *src++ ) != '\0' );
return dest;
}
and can be called like
char s1[13] = "Hello ";
const char *s2 = "World!";
puts( my_strcat( s1, s2 ) );
Another way to define the function using an approach similar to yours is the following
char * my_strcat( char *dest, const char *src )
{
size_t i = 0;
while ( dest[i] != '\0' ) ++i;
for ( size_t j = 0; src[j] != '\0'; j++ )
{
dest[i++] = src[j];
}
dest[i] '\0';
return dest;
}

Error in checking if pointer reaches end-of-string

I am working on a string reversal problem where I am trying to swap values from two ends with one another. As a part of the reverse function, I have a line that checks whether the pointer has reached the end-of-string character (in the form of while(*end!= '\0')). However, this doesn't seem to be working, and at the end of the while loop when I de-reference "end" I get blank. When I use (while(*end)), everything works perfectly but I have to then decrement my pointer "end" to make sure I am accessing the last element of my string. Why can't I check the pointer against the string literal '\0'?
#include<stdio.h>
void reverse(char* s);
void main(){
char str[]="abcdef";
printf("%s\n",str);
reverse(str);
printf("%s\n",str);
}
void reverse(char* p){
char* start = p;
char* end = p;
char tmp;
int length =0;
while(*end!='\0'){
end+=1;
length+=1;
}
printf("%c\n",*end); // problem line
int c;
for (c = 0; c < length/2; c++)
{
tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
//printf("%s\n",p);
}
In the //Problem line the value of *end is '\0' - You should print the integer value of '\0' to verify which is 0 & it works - apart from that you'll need to uncomment the } from reverse function.
'\0' is a non printable character: Reference: Non-printable and Printable ASCII Characters
#include <stdio.h>
#include <string.h>
size_t mystrlen(const char *str)
{
const char *ptr = str;
while(*ptr++);
return ptr - str;
}
char *reverse(char *str)
{
size_t len = mystrlen(str);
char *end = str + len -1;
char *saved = str;
len /= 2;
while(len--)
{
char tmp = *str;
*str++ = *end;
*end-- = tmp;
}
return saved;
}
int main(void)
{
char str[] = "This is the string which will be reversed";
printf("%s\n", reverse(str));
}
your code works. end reaches '\0'. but printf prints string until the first '\0'. so your print appears empty. if you add after the while loop:
--end;
or change the while to: while(*(end+1))
your code will do what you want it to
You don't need the length variable, and you can use pre-decrement on end.
#include <stdio.h>
void reverse(char *start);
int main(void) {
char str[]= "abcdef";
printf("%s\n", str);
reverse(str);
printf("%s\n", str);
}
void reverse(char* start) {
char *end = start;
while(*end != '\0') {
end++;
}
while(start < end) {
char temp = *--end;
*end = *start;
*start++ = temp;
}
}

How to change the content of pointer without changing its pointing address?

I am trying to append a string t in a string s using pointers.
#include<stdio.h>
#include "my_functions.h"
int main(void)
{
char* s = "H"; /*First string*/
char* t = "W"; /*String to be appended*/
char* buff; /*used to store the initial value of s pointer*/
buff = s;
while(*s != '\0')
{
s++;
}
/*At the end of the loop I will get a memory add where string s
ends*/
/*Start appending second string at that place*/
char* temp; /*Creating a temporary pointer to store content
value of second string*/
temp = t;
t = s; /*t now points to the new starting position for
appending operation*/
*t = *temp; /*------ERROR-------*/
/*Rest is to just print the whole appended string at
once*/
s = buff;
while(*s!='\0')
{
printf("%c\t",*s);
s++;
}
printf("Works Fine");
return 0;
}
I am not getting any output on the terminal, nor any errors. The Idea is to change the content of new location of t which is '\0' to the content of t to be appended that is 'W'. I am a noob. Any suggestions? Where Am I wrong?
I suggest you to study this code:
char *strcat(char *dest, const char *src)
{
char *ret = dest;
while (*dest)
dest++;
while (*dest++ = *src++)
;
return ret;
}
Obviously the input parameter dest MUST be a pointer that points a memory area at least big as the sum of the two strings length (pointed by dest and src) + 1 byte (the 0 terminator).

How to copy a character array into another character array in 'c' using pointers and without using string library

How do I copy a character array into another character array in C using pointers and without using string library?
I've tried this:
Header File:
char strconcat(char a[100], char b[100]) {
int i=0,j=0;
while(a[i] != '\0'){
i++;
}
while(b[j] != '\0'){
a[i] = b[j];
j++;
i++;
}
return a[100];
}
char *strconcat(char *dst, const char *src)
{
char *bak = dst;
while (*dst) dst++;
while (*dst = *src)
{
dst++;
src++;
}
return bak;
}
Note you cannot have char a[100] as your function parameters. In C this notation is automatically converted to char a[] that is equivalent to char *a.
You also don't have to compare things to 0. Omitting a comparison will make that happen by default.
void strconcat(char *a, char *b)
{
int i=0,j=0;
while(a[i++]!='\0')
;
while(b[j]!='\0')
a[i++]=b[j++];
a[i] = '\0';
}
The first version naively assumes enough space in destination,
char *
my_strcat(char *destp, char *srcp)
{
if(!destp) return(destp); //check that dest is valid
if(!srcp) return(destp); //check that src is valid
char *dp=destp; //save original destp
while(*dp) { dp++; } //find end of destp
while(*srcp)
{
*dp++ = *srcp++; //copy src to dest, then increment
}
return(destp);
}
The second version allows you to specify maximum size for destination,
char *
my_strncat(char *destp, char *srcp, long max)
{
if(!destp) return(destp); //check that dest is valid
if(!srcp) return(destp); //check that src is valid
char *dp=destp; //save original destp
long x=0;
while(*dp && (x<max)) { dp++; ++x; } //find end of destp
while(*srcp && (x<max)) //copy from src while more space in dst
{
*dp++ = *srcp++; ++x; //copy src to dest, then increment
}
*dp = '\0'; //ensure destination null terminated
return(destp);
}

Resources