Reverse Function Array String - c

Why is my reverse function not reversing my string back to Hello! when i am calling my function the second time?
What could i change to use my function to reverse + dereverse my string multiple times?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char arr[], int numberOfChars);
int main()
{
char myString[] = "Hello!";
int length = strlen(myString);
for (int i = 0; i < length; i++)
{
printf("%c", myString[i]);
}
printf("\n");
reverse(myString, length);
reverse(myString, length);
getchar();
return 0;
}
void reverse(char arr[], int numberOfChars)
{
for (int i = numberOfChars - 1; i >= 0; i--)
{
printf("%c", arr[i]);
}
getchar();
}

Because you never modify arr[] inside reverse(), you just print it in the order you iterate.
It should be something like:
void reverse(char *s, size_t length)
{
for(size_t i = 0; i < length / 2; ++i)
{
const size_t tail = length - (i + 1);
const char tmp = s[tail];
s[tail] = s[i];
s[i] = tmp;
}
}
Also no weird getchar() call in reverse(), that makes no sense.

If you want to reverse the words "Hello!", it is more convenient to define it as string and then use reverse(myString.begin(), myString.end()) orreverse(&myString[0], myString.length())
The parameter of the functionreverse(p1, p2) should be reverse(first_position, last_position)
Besides, if you really want to define the string as char, you should put them inside iteration and then reverse them one by one as #unwind said.
More details about the reverse function can be found at http://www.cplusplus.com/reference/algorithm/reverse/

You are using the same string the second time as well. Your function is void, you practically are doing nothing with the string itself, just printing the letters in reverse order. For what you want, you should save what you are doing in a variable and then return it, and maybe use a function to print separately.

The argument (myString) to the reverse function is same in two times when you called from the main function. You are just printing the char in reverse format instead of reverse in sub module.

Related

Returns a new string with characters from two other strings

Write a function common_char that takes two strings as arguments and returns a new string that contains a single copy of all characters that appear in either of the two strings.
For example, string1: hello; string2: world; the new string is : hellowrd (o and l were already in array from hello).
May use string function here.In other words, all characters in string1 are copied into the new string, but characters in string 2 are copied only characters that are not in string1. That is past exam question and the university did not provide answer. Here is my code.
#include <stdio.h>
#include <string.h>
char *common_char(char *string1, char *string2) {
int str_length1 = strlen(string1);
int str_length2 = strlen(string2);
char *new_string = malloc(str_length1+str_length2+1);
for (int index_1 = 0; index_1 < str_length1; index_1++) {
for (int index_2 = 0; index_2 < str_length2; index_2++) {
if (string1[index_1] == string2[index_2]) {
}
}
}
}
int main(void) {
return 0;
}
My idea is to find duplicate characters in string 2 and string 1 according to the nested loop, but there is a problem with the conditional statement, there is red line, also how to copy the character of the non-duplicate string? I know strcopy(), but how to remove the repeated characters?
I've come up with a solution that uses dynamic memory and resizes the result char* each time a new char must be added. There are two loops, the first iterates the b string and the second loop checks that non of char of the b string is repeated in the a string, if it is not repeated, then adds it. Hope you understand the realloc to resize dynamically the char* each time it must be added an element.
Firstly I initialize the result string to the size of string a so it can be all copied inside. The ordering method I think it is called bubble method.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* common_char(char* a, char* b) {
char* result = (char*)malloc(sizeof(char)*strlen(a)+1);
int i = 0;
int j = 0;
int repeated = 0;
strcpy(result,a);
for(i=0; i<strlen(b); i++) {
for(j=0; j<strlen(result); j++) {
if(b[i] == a[j]) {
repeated = 1;
}
}
if(!repeated) {
result = (char*)realloc(result,strlen(result)+sizeof(char));
result[strlen(result)] = b[i];
result[strlen(result)+1] = '\0';
}
repeated = 0;
}
return result;
}
int main()
{
char a[] = "hello";
char b[] = "world";
char* result = common_char(a,b);
printf("%s", result);
return 0;
}
EDIT: I've modified the code to make it function. About the comment of memory allocation, I've modified the declaration of result to give it space for the '\0'. When doing the realloc, I've already considered that the realloc does not increment the strlen() because strlen() is a counter till the '\0' not of the size of the variable.

Reversing a string in C without the output being null

I am trying to reverse a string (character array) using the following code, but when I attempt to print the string, the value of null. This is a homework assignment, but I am trying to learn so any help would be appreciated.
void input_reverse_string(const char* inputStr, char* reverseStr)
{
int i = 0;
int length = 0;
for (; *(inputStr++) != '\0'; i++)
{
length++;
}
while (*inputStr)
{
*reverseStr = *inputStr;
inputStr++;
reverseStr++;
}
const char* chr_ptr = &inputStr[length - 1];
printf("I see a %s\n", *chr_ptr);
*reverseStr = '\0';
printf("%d", length);
/* return reverseStr; */
}
Several things are out of order:
That's a strange way of computing the length of a string. You are using an index variable that you don't need, and incrementing 3 things at the same time, it's unneeded to say the least.
After calculating the length, and incrementing the inputStr pointer up to its end, you don't reset the pointer, so it still points to the end of the string (actually, one after the end!).
Inside the while you are advancing both pointers (inputStr and reverseStr) in the same direction, which can't possibly be right if you want to reverse the string.
The correct way to do this would be:
Compute the length of the string. Either use strlen() or do it by hand, but you really only need to increment one variable to do this. You can avoid incrementing inputStr, just use a temporary pointer.
Start from inputStr + length and walk backwards. Either use a pointer and do -- or just index the string).
Here's a working example:
void reverse_string(const char* inputStr, char* reverseStr) {
unsigned len = 0;
int i;
while (inputStr[len])
len++;
for (i = len - 1; i >= 0; i--) {
reverseStr[len - i - 1] = inputStr[i];
}
reverseStr[len] = '\0';
}
int main(void) {
char a[6] = "hello";
char b[6];
reverse_string(a, b);
puts(b);
return 0;
}
Output:
olleh

exercise 9.7 Kochan. strange output

I am working to learn C using Kochan's Programming in C 4th edition. problem 9.7 the goal is to insert a string of characters into another array. I am supposed to write a function to accomplish this. I have two problems.
When I have the algorithm print the result as it goes through the if statements, it produces the desired output, however when I change it to an %s, I only get a partial output. My hunch is that a null character is being placed where i do not want it, but I simply cannot see it.
To see what was happening, I added a printf that would track the letter and the array space it was occupying. I was surprised to see that the first letter was not 0, but was blank, and the next letter was assigned the 0. Any insight into this would be appreciated.
The funtion of interest is "insertString".
#include <stdio.h>
#include <stdbool.h>
char x[] = {"the wrong son was shot that day"};
char text[] = {"per"};
int countString (char x[])
{
int counter, z;
for (counter = 0; x[counter] != '\0'; ++counter)
z = counter+1;
return z;
}
void insertString (char text[],char x[],int n) //source, text to input, where
{
int count, clock, i = countString(text), q = countString(x);
int counter = 0;
char y[i + q];
for(count = 0; x[count] != '\0'; ++count){
if (count < n){
y[count] = x[count];
printf("%c %i", y[count], count); //The integer call is just to put a number next to the
//letter. This is where my second issue is shown.
}
else if (counter <= i){
y[count] = text[counter];
++counter;
printf("%c", y[count]);
}
else{
y[count]= x[count - counter];
printf("%c", y[count]);
}
}
printf("\n\n");
y[count-counter] = '\0';
printf("%s", y);
}
int main (void)
{
void insertString(char text[], char x[], int i);
int countString(char x[]);
int i;
insertString(text, x, 10);
return 0;
}
10 out of 10 times I post here it is because im doing something dumb, so I use SO as an absolute last resort if i am getting into the territory of just randomly trying stuff with no methodology. Thanks for your patience in advance.
Your condition is wrong in the for. It should be x[count - counter] != '\0'
In the second condition use just < to avoid overindexing. (else if (counter < i))
You put the terminating NULL char at wrong place. You should do this: y[count] = '\0'
printf inside a string routine like this is fine for debugging, but it's a poor way to write a general-purpose function because it makes it impossible to use its output for further programmatic manipulation. It can also make it difficult to reason about how the state of the function interacts in unpredictable ways with the state of the printed data.
I assume you haven't learned about dynamic memory allocation which is a prerequisite to returning strings from functions. You can inline the function logic into main or printf only at the end of the function in the meantime.
Adding to this point, a void function would need to reallocate space in the string to insert into and would be in-place. This seems likely less generally useful than allocating a new string to hold the result.
Using global variables like char x[] when there's no need is poor practice. It's better to put those strings scoped to main. Since your function can access these variables in addition to its parameters, confusion can ensue when scope and encapsulation is breached.
Use consistent formatting and avoid variable names like q that mean virtually nothing. Instead of adding comments to explain poor var names:
void insertString (char text[],char x[],int n) //source, text to input, where
You can simply name the variables exactly what they represent:
void insertString(char *dest, char *source, int add_index)
Also, now that you've mastered countString, you can abstract this by calling the builtin strlen.
Be sure to allocate enough space in buffers: char y[i + q]; should be y[i+q+1] to allow room for the null terminator '\0'.
As for the logic, I think it's easier to break into three loops without conditions instead of one loop with conditions. This makes it easier to break the problem down into the three constituent steps:
Add everything up until add_index from the dest string to the result.
Add everything in the source string to the result.
Add everything after add_index from the dest string to the result.
Using this approach, all that's left is figuring out how to map the indexes appropriately. Here it is in code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *insert_string(char *dest, char *source, int add_index) {
int source_len = strlen(source);
int dest_len = strlen(dest);
int result_size = source_len + dest_len + 1;
char *result = malloc(result_size);
for (int i = 0; i < add_index; i++) {
result[i] = dest[i];
}
for (int i = 0; i < source_len; i++) {
result[i+add_index] = source[i];
}
for (int i = add_index; i < dest_len; i++) {
result[i+add_index] = dest[i];
}
result[result_size-1] = '\0';
return result;
}
int main(void) {
char *result = insert_string("hello world", "cruel ", 6);
printf("%s\n", result);
free(result);
return 0;
}
Although this is likely for instructional purposes, these operations can be abstracted further using builtin string functions like strncpy and sprintf.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *insert_string(char *dest, char *source, int add_index) {
int result_size = strlen(dest) + strlen(source) + 1;
char *result = malloc(result_size);
char pre[add_index+1];
pre[add_index] = '\0';
strncpy(pre, dest, add_index);
sprintf(result, "%s%s%s", pre, source, dest + add_index);
return result;
}
int main(void) {
char *result = insert_string("hello world", "cruel ", 6);
printf("%s\n", result);
free(result);
return 0;
}
Doing this in-place is more straightforward. Since the result already has the prefix, you can copy the destination postfix to create a source-sized gap in the middle and then overwrite the gap using the source string. It's up to the caller to make sure that the destination buffer is large enough to hold the insertion.
#include <stdio.h>
#include <string.h>
void insert_string(char *dest, char *source, int add_index) {
int source_len = strlen(source);
int dest_len = strlen(dest);
for (int i = add_index; i < dest_len; i++) {
dest[i+add_index] = dest[i];
}
for (int i = 0; i < source_len; i++) {
dest[i+add_index] = source[i];
}
}
int main(void) {
// allocate extra space in the string to hold the insertion
char greeting[32] = "hello world";
insert_string(greeting, "cruel ", 6);
printf("%s\n", greeting);
return 0;
}
A note of caution: none of these functions handle errors at all, so they're unsafe. Correct functions should check that the add_index falls within the bounds of the dest string. This is an exercise for the reader.
The original exercise is here:
Your function is not doing it. You need to insert the string into another string not to create a new one with both mixed. You can do it this way of course and then copy it into the original one - but it is the most uneficient way to archive it (memory & timewise).
Use the correct types.
size_t mystrlen(const char *str)
{
const char *end = str;
while(*end++);
return end - str - 1;
}
char *strinsert(char *dest, size_t pos, const char *istr)
{
char *temp = dest, *work;
size_t ilen = mystrlen(istr);
size_t nmove;
while(*temp) temp++;
nmove = temp - dest - pos + 1;
work = temp;
temp += ilen;
while(nmove--) *temp-- = *work--;
work = dest + pos;
while(*istr) *work++ = *istr++;
return dest;
}
int main()
{
char dest[128] = "0123456789012345678901234567890123456789";
printf("%s", strinsert(dest, 7, "ABCD"));
}
https://godbolt.org/z/KMnLU2

How do you modify a string in C

I'm new to C and I'm attempting to perform a modification to a string that I have (I'm attempting to reverse it), but I'm unsure why the program doesn't run as intended (displays no output).
void reverse(char *rTarget);
int main()
{
char memes[] = "memes";
reverse(memes);
printf("%s", memes);
}
void reverse(char *rTarget)
{
char swap;
int length = (int) strlen(rTarget);
for (int i = 0; i < length / 2; i++)
{
swap = rTarget[i];
rTarget[i] = rTarget[length - i];
rTarget[length - i] = swap;
}
}
For starters this function declaration
void reverse(char *rTarget[]);
is equivalent to
void reverse(char **rTarget);
There is no great sense to declare it such a way.
You should declare the function like
void reverse( char rTarget[] );
and call it like
reverse( memes );
The function definition is also wrong. At least you have to use the loop like
for (int i = 0; i < length / 2; i++)
and these expressions
swap = *rTarget[i];
*rTarget[i] = *rTarget[length - i];
*rTarget[length - i] = swap;
are invalid. They shall be rewritten like
swap = ( *rTarget )[i];
( *rTarget )[i] = ( *rTarget )[length - i -1];
( *rTarget )[length - i - 1] = swap;
Also the variable length shall have the type size_t because in general an object of the type int can not accommodate an object of the type size_t and the function strlen have the return type size_t. So you have to substitute this declaration
int length = (int) strlen(*rTarget);
For this declaratiuon
size_t length = strlen(*rTarget);
Pay attention to that according to the conventions for standard C string functions the function should return pointer to the reversed string.
And according to the C Standard the function main without parameters shall be declared like
int main( void )
Here is a demonstration program that shows how the function can be declared and defined
#include <stdio.h>
#include <string.h>
char * reverse( char *s )
{
for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n-i-1];
s[n-i-1] = c;
}
return s;
}
int main(void)
{
char memes[] = "memes";
puts( reverse( memes ) );
return 0;
}
The program output is
semem
#include <stdio.h>
#include <string.h>
void reverse(char *rTarget);
int main()
{
char memes[] = "memes";
reverse(memes);
printf("%s", memes);
}
void reverse(char *rTarget)
{
int length = (int) strlen(rTarget);
for (int i = 0; i < length / 2; i++)
{
char swap = rTarget[i];
rTarget[i] = rTarget[length - i - 1];
rTarget[length - i - 1] = swap;
}
}
The expression *rTarget[i] is parsed as *(rTarget + i), not *(rTarget) + i as you may expect. And you do not need to pass char ** here.
The problem is here
*rTarget[i] = *rTarget[length - i]
*rTarget[length - i] = swap;
The primary problem seems to be operator precedence. [] has higher precedence than *. You are passing "array of strings" (because pointers can be used as arrays), so when i is 0, on the left side you take the first (and only existing) array at [0], and * references its first character. To this you assign nonsense from the right side: first character of array [length] (which doesn't exist), which is undefined behavior, so what you program does after this is meaningless to speculate about. You need to add parentheses, so that you reference the pointer to array, so you get the actual array, and then use [] on that.
But even if you add parentheses, your algorithm is wrong. On first round, i is 0 so you copy (*rTarget)[length] to start of string. That is the string terminating 0, so you end up with modified string length 0 (first byte is 0). You need -1 for the length.
Fixing your original code, it therefore becomes:
for (int i = 0; i < length / 2; i++)
{
swap = (*rTarget)[i];
(*rTarget)[i] = (*rTarget)[length - i - 1];
(*rTarget)[length - i - 1] = swap;
}
Additionally, your function argument is needlessly complex (pointer to pointer to char), straight pointer to modifiable string is enough. Other answers cover that beauty issue.
There are all kinds of dereferencing errors here. Rememember, a string is an array of characters, not an array of character pointers. So this
void reverse(char *rTarget[])
Should be
void reverse(char rTarget[])
And here:
int length = (int) strlen(*rTarget);
You don't have to dereference rTarget, so remove the *. Same in the swap, remove all * that you have there.
Last but not least, there's an off by one error in your swap. Instead of going to length - i, you want to go to length - i - 1, otherwise you're swapping the null terminator to the beginning.
Also your loop should only go to the first half of the array, because otherwise you swap everything twice, resulting in the original string again:
for (int i = 0; i < length/2; i++)
All in all it should look like this:
void reverse(char rTarget[]);
int main()
{
char memes[] = "memes";
reverse(memes);
printf("%s", memes);
}
void reverse(char rTarget[])
{
char swap;
int length = (int)strlen(rTarget);
for (int i = 0; i < length/2; i++)
{
swap = rTarget[i];
rTarget[i] = rTarget[length - i - 1];
rTarget[length - i - 1] = swap;
}
}
This should do it:
void reverse(char rTarget[]);
int main()
{
char memes[] = "memes";
reverse(memes);
printf("%s", memes);
}
void reverse(char rTarget[])
{
char swap;
int lastindex = (int) strlen(rTarget) - 1;
for (int i = 0; i < lastindex / 2; i++)
{
swap = rTarget[i];
rTarget[i] = rTarget[lastindex - i];
rTarget[lastindex - i] = swap;
}
}
There are a couple of problems in your code.
First of all, arrays are always 'passed by reference', so you don't need to pass a pointer to an array. Any changes you make in the array will persist after the function call.
Secondly, array subscripts in C start from 0. So the last character (not counting null) in a string of length n is string[n-1] not string[n].
EDIT: As #roottraveller mentioned, since you are swapping characters, you don't need to run till the end of the string. You only need to go till the half of length.
If you are not intended to send multiple strings to reverse function then function argument only require a char pointer. While swap character you need to traverse only 50% of the string. The code can be as following.
void reverse(char *rTarget)
{
char swap;
int length = (int) strlen(rTarget);
for(int i = 0, j = (length - 1); i < (length / 2); i++, j--)
{
swap = *(rTarget+i);
*(rTarget+i) = *(rTarget+j);
*(rTarget+j) = swap;
}
}

Strange printf output in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * reverse(char *string);
int main(int argc, char *argv[])
{
char array[10];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = 'd';
array[4] = 'e';
printf("1%s\n",array);
char *p = reverse(array);
printf("4%s\n",p);
printf("5%s\n",array);
}
char * reverse(char *string)
{
int size = strlen(string);
char reversed[size];
int i;
int j = 0;
for(i = size-1; i >= 0; i--)
{
reversed[j] = string[i];
j++;
}
printf("2%s\n",reversed);
string = reversed;
printf("3%s\n",string);
return reversed;
}
This code basically just initializes an array of values and passes it into a method that reverses these values.
I am not sure if this is the best way to execute the task, since I am new to pointers and arrays in C.
But the real question is this:
Can anyone figure out why in this line
printf("4%s\n",p);
if you remove the preceding '4', so it looks like so
printf("%s\n",p);
the line won't print at all?
You are returning a pointer to local variable(reversed) in the function reverse the question should actually be: Why did it work in the first place?.
This code string = reversed; will only copy the pointer, and again the local copy of the pointer so it has no effect outside the function.
To reverse a string you don't need additional memory - this can be done in-place.
Strings in C must end with the null character. You're using strlen on a non null-terminated string.
Furthermore, you just a very lucky person, because there is a serious problem with you code: you forget to add \0 symbol at the end of string.
UPD: the main problem is with code line char reversed[size];.
It's a regular local variable, it has automatic duration, which means that it springs into existence when the function is called and disappears when the function returns (see this link).
You need to change it to:
char *reversed = malloc((size+1)*sizeof(char));
UPD-2: another bug fixing will be:
1) add array[5] = '\0'; after all other array initializing lines
2) add reversed[j] = '\0'; after for...loop:
for(i = size-1; i >= 0; i--)
{
reversed[j] = string[i];
j++;
}
reversed[j] = '\0';
UPD-3: But in general it will much more correctly initialize your string in appropriate way:
char array[10] = "abcde";

Resources