Remove multiple characters from string - c

I'm trying to creat a program that removes all occurences from a character that I choose from a certain string and also returs the number of characters that were removed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIMV 10
int eliminar(char texto[], char ch, char novoTexto[]){
int a=0,i;
for(i=0; texto[i] != '\0';i++){
texto[i] = texto[i+a];
novoTexto[i]=texto[i];
if(novoTexto[i]==ch){
novoTexto[i] = '\0';
a++;
}
}
return a;
}
int main()
{
char frase[]="Uma arara torta!";
char res[50];
printf("%s\n",frase);
printf("%d\n",eliminar(frase,'r',res));
printf("%s\n",res);
return 0;
}
When I run the program, it returns:
Uma arara torta!
3
Uma a
What I wanted to return is:
Uma arara torta!
3
Uma aaa tota!
It's a silly question but I can't find the mistake that I made. Thank you!

Putting '\0' means to terminate the string there.
You have to skip adding characters to eliminate.
Try this:
int eliminar(char texto[], char ch, char novoTexto[]){
int a=0,i,j=0;
for(i=0; texto[i] != '\0';i++){
if(texto[i]==ch){
a++;
}else{
novoTexto[j]=texto[i];
j++;
}
}
novoTexto[j]='\0';
return a;
}

Putting '\0' means to terminate the string there.
check this:
int eliminator(char * texto, char ch, char * novoTexto) {
int i=0;
while(*texto != '\0') {
if (*texto == ch) {
texto++;
++i;
} else {
*novoTexto++ = *texto++;
}
}
*novoTexto = '\0';
return i;
}
int main(int arg, char **argv) {
char frase[] = "Uma arara torta!";
char res[50];
printf("%s\n",frase);
printf("%d\n",eliminator(frase, 'r', res));
printf("%s\n",res);
}

Related

Not Getting Any output While Printing after combining strings

To combine First , middle and last name of a person.
int main()
{
int i,j,k;
char first_name[11]="Gursheesh";
char middle_name[10]="Singh";
char last_name[10]="Chawla";
char name[30];
for(i=0;first_name!='\0';i++)
{
name[i]=first_name[i];
}
name[i]=' ';
i++;
for(j=0;middle_name!='\0';j++)
{
name[i+j]=middle_name[j];
}
name[i+j]=' ';
j++;
for(k=0;last_name!='\0';k++)
{
name[i+j+k]=last_name[k];
}
name[i+j+k]='\0';
printf("%s",name);
}
this is the code
the compiler at run time shows nothing
I cannot find the mistake,help me.
is it having some loop continue this infinity or some logical misktake.
All your loops are infinite:
for(i=0;first_name!='\0';i++)
condition first_name!='\0' is always true. You should check for first_name[i] != '\0':
for(i=0; first_name[i] != '\0'; i++)
By the way, to copy strings it's better to use strcpy function.
Cleaner approach using pointers and storing inputs in an array.
#include <stdio.h>
int main (void)
{
char result[30];
char *pntResult = result;
char *appendStr (char *dest, char *strToAppend);
char *name[3] = { "FirstName", "MiddleName", "LastName" };
int i;
for ( i = 0; i < 3; ++i )
pntResult = appendStr (pntResult, name[i]);
*(pntResult - 1) = '\0';
printf ("%s\n", result);
return 0;
}
char *appendStr (char *dest, char *strToAppend)
{
while ( *strToAppend )
*dest++ = *strToAppend++;
*dest++ = ' ';
return dest;
}
Here is the correct code.
#include <stdio.h>
int main(){
int i,j,k;
char first_name[11]="Gursheesh";
char middle_name[10]="Singh";
char last_name[10]="Chawla";
char name[30];
for(i=0;first_name[i]!='\0';i++)
{
name[i]=first_name[i];
}
name[i]=' ';
i++;
for(j=0;middle_name[j]!='\0';j++)
{
name[i+j]=middle_name[j];
}
name[i+j]=' ';
j++;
for(k=0;last_name[k]!='\0';k++)
{
name[i+j+k]=last_name[k];
}
name[i+j+k]='\0';
printf("%s",name);
}

Need to write a C Program to remove repeated characters adjacent to each other

I need to remove only the repeated characters that are adjacent to each other.
Example: if the input is "heeellooo wooorllldd", the output should be "helo world". The output I am currently getting is "helo wrd".
This is the code i have.
#include <stdio.h>
#include <string.h>
main()
{
char str[]="heeello wooorld";
redundant(str);
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check])
{
ch = str[check];
i = j = check + 1;
while(str[i])
{
if(str[i] != ch)
{
str[j] = str[i];
j++;
}
i++;
}
str[j]='\0';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
I was looking for a minimalistic solution. Just for fun.
void redundant(char *str) {
int lastch = -1; /* last read character */
char* inpp = str; /* pointer to input location */
char* outp = str; /* pointer to output location */
while (*inpp != '\0') {
if (*inpp != lastch) {
*outp++ = lastch = *inpp;
}
inpp++;
}
*outp = '\0';
printf("String after removing duplicates : %s\n", str);
}
Whats happening is that your in code a character is taken and then the entire string is checked if the same character is present again .If it is present it is deleted .Therefore your program has only one copy of each character instead of deleting adjacent same characters.
Try this code instead :
#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <conio.h>
void redundant(char *);
main()
{
clrscr();
char str[]="heeello wooorld";
redundant(str);
getch();
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check]) {
j=i=check;
ch= str[check+1];
if(str[check] == ch)
{
i++;
check--;
}
while(str[i]) {
str[j] = str[i];
j++;
i++;
}
str[j]='\0';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
In my code i check if the adjacent character is same or not.If yes I copy the entire string from the next to next position instead.
You could have shortened code by using strcat function as shown :
void redundant(char *str)
{
int check=0;
while(str[check]) {
if(str[check] == str[check+1])
{
str[check+1]='\0';
strcat(str,str+check+2);
check--;
}
check++;
}

Whitespace replace in C language

What is the right way to replace a white space with _ in string passCode with 2 characters?
In the end it should input/output: (a ) → (a_). Is there a way to do this using the isspace?
isspace(passCode[2]) == 0;
Check if the character is a space if yes, then replace it with _.
For example:
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
unsigned char str[]="a ";
while (str[i])
{
if (isspace(str[i]))
str[i]='_';
i++;
}
printf("%s\n",str);
return 0;
}
A simple manner for character substitution is simply to create a pointer to the string and then check each character in the string for value x and replace it with character y as you go. An example would be:
#include <stdio.h>
int main (void)
{
char passcode[] = "a ";
char *ptr = passcode;
while (*ptr)
{
if (*ptr == ' ')
*ptr = '_';
ptr++;
}
printf ("\n passcode: %s\n\n", passcode);
return 0;
}
output:
$ ./bin/chrep
passcode: a_
The best way to do this is:
#include <stdio.h>
void replace_spaces(char *str)
{
while (*str)
{
if (*str == ' ')
*str = '_';
str++;
}
}
int main(int ac, int av)
{
char *pass = 'pas te st';
replace_spaces(pass);
printf("%s\n", pass);
return (0);
}
pass i now equal to 'pas_te_st'.

Finding last character

I'm new to C and trying to create a function that check a string and returns the last character.
I get the function to print the correct letter, but I cant figure out how to return it :/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char last_chr(char *c);
int main (int argc, const char * argv[]) {
char *text[15];
strcpy(text, "*find:last;char#");
last_chr(text); //debugging
//printf("last char: %c", last_chr(text)); //not working
return 0;
}
char last_chr(char *c) {
char *endchr;
char result;
int pos = strlen(c)-1;
endchr = c[pos];
//sprintf(result,"%s",endchr); //"EXEC_BAD_ACCESS"
putchar(endchr); //prints #
//putc(endchr, result); //"EXEC_BAD_ACCESS"
//printf(endchr); //"EXEC_BAD_ACCESS"
return result;
}
You don't assign result. You probably mean
result = c[pos];
instead of endchr = c[pos];
endchr is a character-pointer instead of a character.
#include <stdio.h>
#include <string.h>
char last_chr(char *c);
int main (int argc, const char * argv[]) {
char text[32];//char *text[15]
strcpy(text, "*find:last;char#");//length is 17
printf("last char: %c", last_chr(text));//#
return 0;
}
char last_chr(char *c) {
if(c == NULL || *c == '\0') return 0;
return c[strlen(c)-1];
}

Why does my code print the whole string multiple times rather than 1 time?

I'm new to C. I'm having some trouble understanding some fundamental materials in reading input and pointers. I want to use a nextChar() function to read and print each character of a string that I enter in the command line. I try typing "hello"..It displays "hello" 6 times. Can someone tell me why this happens? How can I fix it? Thank you for your time!
#include <stdio.h>
#include <assert.h>
char nextChar(char* ptr)
{
static int i = -1;
char c;
++i;
c = *(s+i);
if ( c == '\0' )
return '\0';
else
return c;
}
void display(char* ptr)
{
assert(ptr != 0);
do
{
printf("%s", ptr);
} while (nextChar(ptr));
}
int main(int argc, const char * argv[])
{
char* ptr=argv[1];
display(ptr);
return 0;
}
The %s format specifier instructs printf to print an array of chars, until it finds a null terminator. You should use %c instead if you want to print a single char. If you do this, you'll also need to use the return value from nextChar.
Alternatively, more simply, you could change display to iterate over the characters in your string directly
void display(char* ptr)
{
assert(ptr != 0);
do
{
printf("%c", *ptr); // print a single char
ptr++; // advance ptr by a single char
} while (*ptr != '\0');
}
Or, equivalently but with less obvious pointer arithmetic
void display(char* ptr)
{
int index = 0;
assert(ptr != 0);
do
{
printf("%c", ptr[index]);
index++;
} while (ptr[index] != '\0');
}
the nextchar function could be reduced:
char nextChar(char* ptr)
{
static int i = 0;
i++;
return (*(ptr+i));
}
and display to
void display(char* ptr)
{
assert(ptr != 0);
char c = *ptr;
do
{
printf("%c", c);
} while (c = nextChar(ptr));
}
#include <stdio.h>
#include <assert.h>
char nextChar(const char* ptr){
static int i = 0;
char c;
c = ptr[i++];
if ( c == '\0' ){
i = 0;
}
return c;
}
void display(const char* ptr){
char c;
assert(ptr != 0);
while(c=nextChar(ptr)){
printf("%c", c);
}
}
int main(int argc, const char * argv[]){
const char* ptr=argv[1];
display(ptr);
return 0;
}

Resources