Ravindian
BAN USERStart from the end, move all non-zero values to end and fill the remaining elements with zeros.
main()
{
int a[11] = {1,9,8,4,0,0,2,7,0,6,0};
int N = 11, i, j;
for(i = N-1, j = N-1; i >= 0; i--)
{
if(a[i] != 0)
{
a[j] = a[i];
j--;
}
}
for(i = 0; i <= j; i++)
{
a[i] = 0;
}
/* Print */
for (i = 0; i < N; i++)
{
printf("%d", a[i]);
}
}
typedef struct _node
{
char c;
struct _node *next;
}node;
main()
{
char *string = "Hello World This is a Sample Program";
node *word = NULL;
while(*string)
{
if(*string != ' ')
{
word = push(word, *string);
string++;
continue;
}
print(word);
printf(" ");
freenode(word);
word = NULL;
string++;
}
printf(" ");
print(word);
}
node* push(node *prev, char chr)
{
node *link = NULL;
link = (node *) malloc(sizeof(node));
link->c = chr;
link->next = prev;
return link;
}
void print(node *word)
{
if(word == NULL)
return;
printf("%c", word->c);
print(word->next);
}
void freenode(node *fnode)
{
if(fnode->next != NULL)
freenode(fnode->next);
free(fnode);
}
Assumption: Input is only characters A-Z, a-z
typedef struct _CharCount
{
char chr;
int count;
}CharCount;
main()
{
CharCount stCharCount[52] = {0};
char *temp = "AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv";
char chr = *temp;
int i = 0;
while(*temp != '\0')
{
if(chr == *temp)
{
stCharCount[i].chr = *temp;
stCharCount[i].count++;
}
else
{
printf("%d{%c}",stCharCount[i].count,stCharCount[i].chr);
i++;
stCharCount[i].chr = *temp;
stCharCount[i].count++;
chr = *temp;
}
temp++;
}
printf("%d{%c}",stCharCount[i].count,stCharCount[i].chr);
}
lol
- Ravindian June 17, 2012