ورود

View Full Version : سوال: حذف صفر



mohammad2407
جمعه 04 بهمن 1392, 03:57 صبح
سلام دوستان چطوری برنامه ای بنویسم که صفر عدد حذف کنه

مثلا وارد کنیم :1020265

خروجی چاپ کنه : 12265

mehran6764
جمعه 04 بهمن 1392, 05:34 صبح
C remove character from string (http://stackoverflow.com/questions/5457608/c-remove-character-from-string)

mohammad2407
جمعه 04 بهمن 1392, 12:21 عصر
راهنمایی !!!!!!!!!!!!!!!!!!!1

کامبیز اسدزاده
جمعه 04 بهمن 1392, 13:21 عصر
به این صورت استفاده کنید :


#include <iostream>
#include <algorithm>
#include <string>

bool IsParenthesesOrDash(char c)
{
switch (c)
{
case '0':
return true;
default:
return false;
}
}

int main()
{
std::string str("1020265");
str.erase(std::remove_if(str.begin(), str.end(), &IsParenthesesOrDash), str.end());
std::cout << str << std::endl; // Expected output: 12265

std::getchar();
}

hamedlll
جمعه 04 بهمن 1392, 13:32 عصر
اینم با C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int j=0;
long num;
char buff[33], text[33];
printf("Enter number: ");
scanf("%ld", &num);
sprintf(buff, "%ld", num);
for (int i = 0; i < strlen(buff); ++i) {
if (buff[i] != '0') {
text[j] = buff[i];
++j;
}
}
printf("%s", text);
}