'Input: a2b3c4 and Output: aabbbcccc
The code I've written is not producing any output. It just takes the string as an input:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main() {
char str[100];
int i,size,s,pos;
scanf("%s", &str);
size=strlen(str);
for(i=0;i<size;i++) {
if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122)) {
i++;
} else {
if(str[i]>='0' && str[i]<='9') {
for(s=0;s<str[i];s++) {
printf("%s", str[i-1]);
}
}
i++;
}
}
}
Solution 1:[1]
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i=0,s;
scanf("%s",str);
while(str[i]!='\0')
{
if(str[i]>='a' && str[i]<='z')
{
i++;
}
else if(str[i]>='A' && str[i]<='Z')
{
i++;
}
else if(str[i]>='0' && str[i]<='9')
{
for(s=0;s<str[i]-'0';s++)
{
printf("%c", str[i-1]);
}
i++;
}
}
}
Solution 2:[2]
This whole code has many errors:
- You try to print a single character with
%s
, which is for strings. This leads to undefined behavior -- the correct conversion for a single character is%c
. - You loop until some "digit character" like
'3'
. You want to loop until the number3
instead. Subtract'0'
to achieve this. - Doing
scanf("%s", ...)
is potential undefined behavior, it will eventually overflow any buffer. You might want to read my beginners' guide away fromscanf()
. In short, at least add a field width, in your casescanf("%99s", ...)
scanf()
expects a pointer to where to put the data, butstr
already evaluates to a pointer to the first array element. Therefore adding&
is wrong here, leading to more undefined behavior.- Always check the return value of functions that might fail. If your
scanf()
fails to convert something, yourstr
stays uninitialized and the followingstrlen()
is undefined behavior. - Your code uses ASCII values, which is very common, but not mandated; this way, it won't work on machines not using ASCII.
It's not even necessary to use a buffer for what you want to achieve, a single character to save the last character read is sufficient, like this (the other issues are fixed in this example as well):
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int c;
int l = EOF;
while ((c = getchar()) != EOF)
{
if (isdigit(c) && isalpha(l))
{
for (int i = 0; i < c-'0'; ++i)
{
putchar(l);
}
}
l = c;
}
putchar('\n');
return 0;
}
As some further advice:
Compile with compiler warnings enabled, e.g. when using
gcc
, add these flags:-std=c11 -Wall -Wextra -pedantic
this would have identified some of the problems in your code already.
Read a good book on C and look up individual functions in manual pages (on a *nix system, try typing
man 2 printf
for example .. you can also just feed it to google and find web versions of these pages)
Solution 3:[3]
Try this simple code
#include<stdio.h>
#include<string.h>
int main() {
char str[50];
int i=0,j,k,c;
printf("Enter String : ");
scanf("%s",&str);
for(i=0;i<strlen(str);) {
for(j=0;j<str[i+1]-'0';j++) {
printf("%c",str[i]);
}
i=i+2;
}
}
Solution 4:[4]
You can follow this code.May be help you.It's similar to your code...
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100],ch;
int i,len,s,pos,k;
scanf("%s", &str);
len=strlen(str);
int number=0;
for(i=0; i<len; i++)
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
for(k=0;k<number;k++)
{
printf("%c",ch);
}
ch=str[i];
number=0;
}
else
{
number=number*10+(str[i]-'0');
}
}
for(k=0;k<number;k++)
{
printf("%c",ch);
}
printf("\n");
}
Solution 5:[5]
Here is a simple code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX 50
void main()
{
char name[MAX],str1[MAX],str3[MAX];
int str2[MAX],i,j,m,t=0;
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
for(j=0,i=0;name[i]!='\0';i=i+2,j++)
{
str1[j]=name[i];
str2[j]=name[i+1]-'0';
}
for(i=0;i<=strlen(str1);i++)
{
for(j=str2[i];j>0;j--)
{
str3[t]=str1[i];
printf("%c",str3[t]);
t++;
}
}
}
Solution 6:[6]
#include<stdio.h>
int main()
{
char str[200] , ch;
int ind , count;
scanf("%s" , str);
int len , start;
for(len=0 ; str[len] ; len++);
start=len ;
ind=count=0;
while( ind < len )
{
ch = str[ind++];
while(str[ind] >= '0' && str[ind] <= '9' )
count = count * 10 + (str[ind++] - '0');
while(count)
{
str[start++] = ch;
count--;
}
}
for(ind=len ;ind < start ;ind++)
str[ind - len]=str[ind];
str[ind - len] = 0;
printf("%s" , str);
return 0;
}
Solution 7:[7]
Here is the brute force approach in C++
#include <iostream>
using namespace std;
int main() {
string s = "a3b3c4";
int n = s.length();
string w = "";
for(int i=0;i<n;i++) {
if(s[i] >= '0' && s[i] <= '9') {
for(int j=0;j<s[i]-'0';j++) {
w += s[i-1];
}
}
}
cout<<w<<endl;
}
The output will be:
aaabbbcccc
Solution 8:[8]
def decompress_str(com):
ls = []
for i in range(0,len(com),2):
times = int(com[i+1])
while times:
ls.append(com[i])
times -= 1
print("".join(ls))
input = "a2b3c4"
decompress_str(input) # output aabbbcccc
Here:
I'm taking every even index(0,2,4) as the alphabet, and printing it for odd index(1,3,5) times. Appending the output to a list.
Later, I'm changing the list to a string using join().
Solution 9:[9]
public static void main(String[] args) {
String s ="A9B3C4D5";
for(int i =0; i < s.length(); i++) {
if(s.charAt(i)>='A' && s.charAt(i)<='Z'){
System.out.print(s.charAt(i));
}else{
int a = s.charAt(i)-48;
for(int j =1; j <a; j++) {
System.out.print(s.charAt(i-1));
}
}
}
}
output :-AAAAAAAAABBBCCCCDDDDD
Solution 10:[10]
Check out this code:-
#include<iostream>
using namespace std;
int main() {
int j = 0, sum = 0, k = 0;
char str[100];
cout << "Enter string:- ";
cin >> str;
for (int i = 0; i <= strlen(str); i++) {
if (i % 2 != 0) {
sum = sum + (str[i] - '0');
for (j = 1; j <= sum; j++) {
cout << str[k];
}
k=k + 2;
}
sum = 0;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Umang Jain |
Solution 2 | |
Solution 3 | |
Solution 4 | Ashim Chakraborty |
Solution 5 | ssz |
Solution 6 | prathi8081 |
Solution 7 | S.B |
Solution 8 | a. rathi |
Solution 9 | |
Solution 10 | Somil Puri |