Remove character from string
Hi all,
the object is to write a function called say remchar that recieves a string say 'Hello' and a character 'o'.
The function should loop through the string look for the character passed 'o' if it exists which it does, rmove the character from the string and pass it pack to main to print out the modified string.
here is what I have so far and won't compile.
Code:
#include <stdio.h>
int main()
{
char strin[10]={'H','e','l','l','o'}; //init a char string called strin
char chr = 'o'; //character var to pass to function to look for
printf("\nstring before function call\t:>%s", strin); //print the string pre call
remchar(strin, chr); //call remchar func
printf("\nstring after function call\t:>%s\n", strin); //string post call
return 0;
}
remchar(strin, chr)
{
int i;
for(i = 0; strin[i] != '\0'; i++) //start for loop
{
if(strin[i] == chr) //test for character
{
strin[i] = strin[i] + 1 ; // write next character back to current position
}
}
}
The compile error is
Quote:
subscripted value is niether array nor pointer
.
I haven't seen this one before!
Also can any one give me a bit of clarity on how to handle modifying arrays etc? I am not looking at pointers yet!
Any help is much appreciated
Thanks
Remove character from string
Hi quzah
ok thankyou understand that now I have made change to the function and declared so that it compiles. When run it prints Hellp because when 'o' is encountered I have added 1 to the value which makes 'o' become 'p' 'Hellp' how ironic!
This is where I probably require some clarity on updating the string before returning, because my test works by finding the character I am looking for its the how to manipulate this string is my issue given the objective stated earlier!
Here is my updated code to address the function issues!
Code:
#include <stdio.h>
void remchar(char *strin, char chr);
int main()
{
char s[10]={'H','e','l','l','o'}; //init a char string
char c = 'o'; //character var to pass to function to look for
printf("\nstring before function call\t:>%s", s); //print the string pre call
remchar(s, c); //call remchar func
printf("\nstring after function call\t:>%s\n", s); //string post call
return 0;
}
void remchar(char *strin, char chr)
{
int i;
for(i = 0; strin[i] != '\0'; i++) //start for loop
{
if(strin[i] == chr) //test for character
{
strin[i] = strin[i] + 1;//this is wrong!!!!
}
}
}
again thanx for input and guidance
Remove character from string
ok I have now made some changes to the function to process the incoming string write each element to an output string and copy the updated string back to original.
However when it runs I get out as follows
Hell?
where does the question mark come from?
can any one offer me some concise accurate answer?
can anyone comment constructively on whether this is a good way of handling this scenario and offer advise?
many thanks and here is the code
Code:
#include <stdio.h>
#include <strings.h>
void remchar(char *strin, char chr);
int main()
{
char s[10]={'H','e','l','l','o'}; //init a char string
char c = 'o'; //character var to pass to function to look for
printf("\nstring before function call\t:>%s", s); //print the string pre call
remchar(s, c); //call remchar func
printf("\nstring after function call\t:>%s\n", s); //string post call
return 0;
}
void remchar(char *strin, char chr)
{
int i, j=0;
char cupdate[10]; //temp storage for updated string
for(i = 0; strin[i] != '\0'; i++) //start for loop
{
if(strin[i] != chr) //check its not the char i want
{
cupdate[j] = strin[i]; //copy element to temp string
j++; //move along the index one
}
else
{
; //do nothing
}
}
j++; //move along the index one
cupdate[j] = '\0'; //terminate the string
printf("strin = %s\tcupdate = %s", strin, cupdate); //show what its doing
strcpy(strin, cupdate); //copy updated string back to original
}