Amazon Interview Question
Software Engineer in TestsCountry: United States
i/p = "This_____is__an_example"
o/p="This_is_an_example".
HTML is removing extra spaces..
No, Example : Here__is_another____example_dude.
TotalSpaceCount = 8.
No of Words=5
SpacesbetweenEachWord=TotalSpaceCount/(NoOfWords-1)
Considering there is no space at the end of a sentence.
Output: "Here__is__another__example__dude."
I'm guessing this question is asking you to modify an input string by removing multiple spaces. If so:
void remove_multiple_spaces(char* str) {
// Check for null or empty string
if(!str || !*str) {
return;
}
// Maintain a 'read' pointer and a 'write' pointer. The 'read' pointer
// will advance past the write pointer when we hit consecutive spaces.
char* read = str;
char* write = str;
while(*read != '\0') {
// Copy from 'read' to 'write' and advance each pointer
char c = *read++;
*write++ = c;
// If we encounter consecutive spaces, advance 'read'
if(c == ' ') {
while(*read == ' ') {
++read;
}
}
}
// Finally, add a trailing null char at the end of the string
*write = '\0';
}
countspace = 0;
writehere = string;
for( char* i = string; *i; i++ )
{
if( *i != ' ' )
*writehere++ = *i;
countspace = 0;
else
if( countspace == 0 )
*writehere++ = *i;
countspace++;
}
*writehere = 0;
hey ariesgirl069, here is the javacode
class TrimSpacesInString{
public static String trimString(String inputString){
if(inputString == null)
return null;
String inputStringT = inputString.trim();
StringBuffer result = new StringBuffer();
char temp;
int spaceCount = 0;
for(int i = 0; i < inputStringT.length(); i++){
temp = inputStringT.charAt(i);
if( temp != ' '){
if(spaceCount > 0)
result.append(' ');
result.append(temp);
spaceCount = 0;
}
else
spaceCount++;
}
return new String(result);
}
public static void main(String args[]){
System.out.println(trimString(" This is an example "));
return;
}
}
public class EqualSpacesInWords {
public static void main(String[] args) {
String s1 = "This is an example";
String s2 = equalSpacesInWords( s1 );
System.out.println( s2 );
}
private static String equalSpacesInWords( String s ) {
String formatted = "";
List<String> listStr = new ArrayList<String>();
String word = "";
char[] a = s.trim().toCharArray();
int countSpaces = 0;
boolean processWord = true;
for( int i = 0; i < a.length; i++ ) {
if( processWord ) {
if( a[i] != ' ' ) {
word += a[i];
if( i == a.length - 1 ) {
listStr.add( word );
}
}
else {
countSpaces += 1;
processWord = false;
}
}
else {
if( a[i] == ' ' ) {
countSpaces += 1;
}
else {
listStr.add( word );
word = "";
word += a[i];
processWord = true;
}
}
}
int formattedSpaces = countSpaces / listStr.size();
while( !listStr.isEmpty() ) {
formatted += listStr.remove(0);
if( !listStr.isEmpty() ) {
for( int i = 0; i < formattedSpaces; i++ ) {
formatted += " ";
}
}
}
return formatted;
}
}
public class CompressStringSpace {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This is Example";
System.out.println(str);
String newStr = compressSpace(str);
System.out.println(newStr);
}
public static String compressSpace(String str){
StringBuffer buffer = new StringBuffer();
char[] charSet = str.toCharArray();
for(int i =0;i<str.length();i++){
if((str.charAt(i)!=' ')||((str.charAt(i) == ' ') && str.charAt(i)!=str.charAt(i+1)))
buffer.append(str.charAt(i));
}
return buffer.toString();
}
}
#include <stdio.h>
- Nitin February 27, 2012#include <stdlib.h>
int main () {
char *s = "This is an example";
char r[100];
int i = 0;
while (*s) {
if (*s != ' ') {
r[i] = *s;
}
else {
r[i] = *s;
while (*(++s) && *s == ' ');
s--;
}
s++; i++;
}
r[i] = 0;
printf ("Result: %s\n", r);
}