How to rearrange values in a string?
I've been trying to manipulate strings as of recently, but with very little success. The program I've been working on is supposed to take a predetermined string and remove the character indicated. I feel like I'm either going about this completely wrong or I'm just missing something a tad bit smaller. I'm not sure how to access the string properly so that I can copy it step by step to another array, skip over the specific character and then continue copying until it hits the null character at the end of the string. Is using subscripts to access the array a bad idea altogether?
Well, here's my code (if you can even call it that!):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main()
{
void rmchr(char s[], char a);
char s = "abracadabra"; // String
char a = "a"; // Character
printf("The string is \"%s\" with %s.\n", s, a);
rmchr(s, a);
printf("The string is \"%s\" without %s.\n", s, a);
system("PAUSE");
return 0;
}
void rmchr(char s[], char a) // Remove 'a' from the string 's'
{
int i = 0; // Counter variable for string 's'
int j = 0; // Counter variable for string 'temp'
char temp = '\0'; // Set string 'temp' to NULL
while(s[i] != '\0')
{
while (s[i] != a)
{
temp[j] = s[i];
j++;
}
i++;
}
strcpy(s, temp);
}