0
votes

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++;
        }
    }
}
8
dear God... please format the code!ΦXocę 웃 Пepeúpa ツ
Your whole code depends on ASCII, not a good thing in general. This line: for(s=0;s<str[i];s++) loops up to the code of your number character, not what you want. you mean for(s=0;s<str[i]-'0';s++) here. Probably MANY other errors.user2371524
printf("%s", str[i-1]); : type of str[i-1] is char. But %s of printf requires char *BLUEPIXY
And what's conio.h about? This isn't standard C and is not needed here at all!user2371524
Please don't add pictures of code. If you don'lt like the code formatting, edit it to show your formatting, but keep it there as text.user2371524

8 Answers

4
votes

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 number 3 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 from scanf(). In short, at least add a field width, in your case scanf("%99s", ...)
  • scanf() expects a pointer to where to put the data, but str 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, your str stays uninitialized and the following strlen() 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)

1
votes

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;
    }
}
0
votes

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");

}

0
votes

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++;
        }

    }
}
0
votes
#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;
}
0
votes

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
0
votes
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:

  1. 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.

  2. Later, I'm changing the list to a string using join().

-1
votes
#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++;

        }

}

}