Program to trim a given character from a string.

Answers were Sorted based on User's Feedback



Program to trim a given character from a string...

Answer / dj

#include<iostream>
using namespace std;
#include<string.h>
int main()
{
char string[]="lhellolollla";
int i=0;
int count=0;
while(i<strlen(string))
{
while(string[i+count]=='l')
count++;
string[i]=string[i+count];
i++;

}
printf("%s",string);
}

Is This Answer Correct ?    5 Yes 0 No

Program to trim a given character from a string...

Answer / sandeep ambekar

//
// MAIN -- Program to Purge a given character from a string.
// I/P example: ./a.out Bangalore a
// -- Sandeep Ambekar

#include <stdio.h>
#define TRUE 1
#define FALSE 0

void
purge_char_from_string (char *str, char *c)
{
int i, k, flag, count; // i/j for index, flag to
keep track of
// 'c' and count for iteration..
char *buf = str; // save the original string
start:
str = buf; // we could enter the loop
again.
i = 0;
k = 1;
count = 0;
flag = FALSE; // INIT variables...
printf ("Input String [%s] [%c] \n", str, *c);

while (str[i] != '\0')
{
printf ("\t (%c) <==> (%c)\n", str[i], str[k]);
if (str[i] == c[0])
{
str[i] = str[k];
flag = TRUE;
count++;
}
else if (flag == TRUE)
{
str[i] = str[k]; // later have a while loop
to find a char
// to which is !c and replace them.
}
i++;
k++;
}
printf ("[%s]\n", buf);
if ((count - 1) >= 1)
goto start;
} // end of
purge_char_from_string ....

//
// MAIN -- Program to Purge a given character from a string.
//

int
main (int argc, char *argv[])
{
if (argc < 3)
{
printf ("Input <String> <char> \n");
return 1;
}
printf (" ## Input string %s : Char [%s]\n", argv[1],
argv[2]);
purge_char_from_string (argv[1], argv[2]);

printf (" Trimmed String ---> %s\n", argv[1]);
return 0;
} // end of main..

Is This Answer Correct ?    2 Yes 2 No

Program to trim a given character from a string...

Answer / dj

in above solution while loop iterates for only 6 times

Is This Answer Correct ?    0 Yes 0 No

Program to trim a given character from a string...

Answer / raju kalyadapu

//Write a program to trim a Character from a Given String
#include<stdio.h>
int main()
{
int i,j;
char str[30],ch;
printf("Enter Any String: ");
gets(str);
printf("
Enter Character to Trim:");
ch=getchar();
for(i=0;i<strlen(str);i++)   
{
while(str[i]==ch)   
{
j=i;
str[i]=str[i+1];
while(j++<strlen(str))
{

str[j]=str[j+1];
}

}


}
str[i]='';
printf("
");
puts(str);
}

Is This Answer Correct ?    0 Yes 0 No

Program to trim a given character from a string...

Answer / dj

best of luck for tomorrow's test

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

what is a NULL Pointer? Whether it is same as an uninitialized pointer?

0 Answers   TISL,


‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.

0 Answers  


write a program which will count occurance of a day between two dates.

1 Answers   IonIdea,


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

0 Answers   L&T,


How will you print TATA alone from TATA POWER using string copy and concate commands in C?

0 Answers   Amdocs, Apps Associates,






logic for x=y^n

1 Answers   Delphi,


what are threads ? why they are called light weight processes ? what is the relation between process and threads ?

1 Answers  


what is event driven software and what is procedural driven software?

0 Answers  


Is a house a shell structure?

0 Answers  


What is a #include preprocessor?

0 Answers  


m=++i&&++j(||)k++ printf("%d"i,j,k,m)

1 Answers   ABC,


The file stdio.h, what does it contain?

0 Answers  


Categories