#include <stdio.h>
int main() {
char str[101];
int i;
int j=1;
scanf("%s", str);
for(i=0; str[i]!='\0'; i++) {
if(i!=0 && i%j==0) {
printf("\n");
j++;
}
printf("%c", str[i]);
}
}
If I input "abcdefg" in this code, I want it printed in turn like stairs.
a(\n)
bc(\n)
def(\n)
g
How fix code?
Try:
#include <stdio.h>
int main() {
char str[101];
int i;
int j=1,k=0;
scanf("%s", str);
for(i=0;str[i]!='\0';i=k+i)
{
for(j=i;j<=i+k && str[j]!='\0';j++)
printf("%c", str[j]);
k++;
if(str[j]!='\0')
printf("\n");
}
}
This could fix your problem
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j=1,k;
char str[100];
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
for(k=0;k<j;k++)
{
if(str[k+i]!='\0')
printf("%c",str[k+i]);
else
exit(1);
}
printf("\n");
j++;
i=k+i-1;
}
return 0;
}
Related
What's wrong with my code? I'm trying to count the numbers in a string after skipping all alphabets.
How to skip the alphabets and only count the numbers?
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if(a[i]>='a'&&a[i]<='z')
{
continue;
}
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}
I switched your usage of scanf to use fgets. This works:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if (isdigit(a[i]))
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t = 0;
scanf("%d", &t);
while(t--)
{
char a[100001];
fgets(a, sizeof(a), stdin);
function(a);
puts("\n");
}
}
If the OP's intent was in fact to count up the groups of digits (in which the definition of 'number' is each contiguous run of digits), this will accomplish the goal:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int function(char a[])
{
size_t i = 0;
int cnt = 0;
// convert all non-digits to whitespace
printf("Converting\n");
for (char *p = a; *p != '\0'; ++p)
{
*p = isdigit(*p) ? *p : ' ';
}
printf("Found Groups: %s\n",a);
// note: strtok mutates the string it is given, it will not be usable after this:
for (char *gr = strtok(a," "); gr != NULL; gr = strtok(NULL, " "))
{
cnt++;
}
printf("Number of Groups: %d\n", cnt);
}
int main()
{
printf("Enter strings (Ctrl-C to end)\n");
while(1)
{
int result = 0;
char a[100001];
scanf("%s",a);
result = function(a);
printf("\n");
}
}
(I was not able to get the version using fgets to work, but how the string is gathered is not really in the scope of the question, the OP's original code in main is functional, I just made it an open-ended loop for my tests)
In your code continue statement is responsible for wrong answer. Because of it your code is not encountering the statement a[i] !='\0'. so suggest you to just avoid it and close your if statement without any code as like me. One more suggestion use also uppercase letters as user can also enter uppercase letters and using fgets() function you can also read white spaces.
Try out this
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if((a[i]>='a'&&a[i]<='z') || (a[i]>='A'&&a[i]<='Z'));
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}
Do anybody knows how to search for a name in a string array? If i register the name 'jacob' and search for cob I need to get jacob shown instead of not showing up anything. I don't know if strcmp is the right way to do it. Any ideas?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
struct name{
char name[MAX];
};
void getRegister(struct name name[], int *nrNames);
void getSearch(struct name name[], int nrNames);
int readLine(char s[], int length);
int main(){
int run=1;
struct name name[MAX];
int nrNames=0;
while(run){
char choice;
printf("\n (1)Register\n(2)Search\n(3)Quit\n");
scanf(" %c%*c", &choice);
if(choice=='1') getRegister(name, &nrNames);
if(choice=='2') getSearch(name, nrNames);
if(choice=='3') run=0;
}
return 0;
}
void getRegister(struct name name[], int *nrNames){
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
(*nrNames)++;
}
void getSearch(struct name name[], int nrNames){
int i;
char input[MAX];
printf("Enter name: ");
readLine(input, MAX);
if(i>=0){
printf("Name/s:\n");
for(i=0; i<nrNames;i++){
if(strcmp(input, name[i].name)==0){
printf("\n%s\n",name[i].name);
}
}
}
}
int readLine(char s[], int length){
int ch, i=0;
while(isspace(ch=getchar()));
while(ch!='\n' && ch!=EOF) {
if(i<length) s[i++]=ch;
ch = getchar();
}
s[i]='\0';
return i;
}
Try to search for the match in the array. The code below displays a position for each occurrence of the second array in the first array. It uses naive approach. There are more efficient algorithms like Knuth-Morris-Pratt or Boyer-Moore algorithm.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
int main(){
char c;
char name[MAX], search_name[MAX];
int i = 0, j = 0, match = 0, count = 0;
printf("Register name: ");
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
name[i++] = c;
}
}
name[i] = '\0';
printf("Search name: ");
i = 0;
while ((c = fgetc(stdin)) != '\n') {
if (i < MAX){
search_name[i++] = c;
}
}
search_name[i] = '\0';
i=-1;
match = 0;
do {
i++;
j = 0;
do {
if (name[i+j] == search_name[j])
match = 1;
else {
match = 0;
break;
}
j++;
} while (search_name[j] != '\0');
if (match)
printf("Match on position %d ", i);
} while (name[i+j] != '\0');
printf("\n");
return 0;
}
I found the soulution myself. Here is the code for those who get stuck as I did.
void searchName(const struct varor reg[], int nrOfGoods){
int i;
char name[20];
printf("Enter name: ");
readLine(name, WORDLENGTH);//gets input
if(i>=0){
printf("\nId.number \t Name \t\t\t Quantity\n");
for(i=0; i<nrOfGoods;i++){
if(strstr(reg[i].name, name)!=NULL){ //this should do the job
printf("%-17d%-24s%-5d\n",reg[i].idnumber,reg[i].name.reg[i].quantity);
}
}
}
}
New C coder here. I'm not certain why, but my program gets stuck in the first while loop and will not move on to the other code. Does anyone have any idea what's wrong?
#include <stdio.h>
int main(void){
char str[20];
char reverse[20];
int c;
int i;
int j;
int k;
printf("Enter a string: ");
i=0;
j=0;
while((i<20)&&((c=getchar())!='\n')){
str[i] = c;
i++;
}
k=i;
while((j<k)&&(i>=0)){
reverse[j]=str[i];
j++;
i--;
}
printf("\n");
if(i==0){
while(i<k){
putchar(reverse[i]);
}
}else{
printf("logic error");
}
return 0;
}
Thank you!
#include<stdio.h>
void reverse(void)
{
char c;
if((c = getchar()) != '\n'){ reverse(); }
putchar(c);
return;
}
int main(void)
{
printf("Enter a line of text below:n");
reverse();
putchar('\n');
}
Your code don't get stuck in the first while. It gets stuck here:
while(i<k){
putchar(reverse[i]);
}
because you never change i or k, i.e. an endless loop.
Try this instead:
while(i<k){
putchar(reverse[i]);
++i;
}
Another problem is that you reverse one character past the input as i has been incremented to index the "next free char" . Don't do that.
Instead of:
while((j<k)&&(i>=0)){
reverse[j]=str[i];
j++;
i--;
}
try:
while(i>=0){
reverse[j]=str[i-1]; // Notice the -1
j++;
i--;
}
++i;
Putting it all together, it will be:
int main(void){
char str[20];
char reverse[20];
int c;
int i;
int j = 0;
int k;
printf("Enter a string: ");
i=0;
j=0;
while((i<20)&&((c=getchar())!='\n')){
str[i] = c;
i++;
}
k=i;
while(i >= 0){
reverse[j]=str[i-1]; // Notice
j++;
i--;
}
++i; // Notice
printf("\n");
if(i==0){ // This if-statement is nor really needed - just remove it
while(i<k){
putchar(reverse[i]);
i++; // Notice
}
}else{
printf("logic error");
}
printf("\n");
return 0;
}
I wanted the two functions to stop the program as the word "exit" is entered, but the loop for that specification is not working. can you please fix this?
Here is the code. please check it.
#include<stdio.h>
#include<string.h>
void uppercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s\n",str);
}
void lowercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("The uppercase equivalent is: %s\n", str);
getchar();
}
int main()
{
char str[100];
char lower[100];
int i;
while(1)
{
printf("Enter any string->");
scanf("%s",&str);
if (str == 'exit')
{
break;
}
else
{
printf("The string is->%s\n",str);
uppercase(str);
lowercase(str);
}
}
return 0;
}
replace:
if (str == 'exit')
by this
if (strcmp(str , "exit")==0)
void uppercase(char *str){
int i;
for(i=0;i<strlen(str);i++){
//if(islower(str[i]))
str[i]=toupper(str[i]);
}
printf("\nThe string in uppercase is->%s\n",str);
}
I want to write a program to create a 2D double array from a text file. The contents and format of my file is
0,0.23645,8.457
4.125,7.102,8.102
1.036,0.547,3.2298,
Same number of row and same number of column. Each number is differentiated by one ','. Each line is differentiated by one'\n' character and at the end one comma.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile
float a,data[10][10];
char ch;
int i,j;
clrscr();
myfile=fopen("E:\\input.txt", "r");
for(i = 0; i<3; i++)
{
for (j = 0 ; j<3; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
It is working, but not for every time. Sometimes it is giving garbage values, zeros or NAN. I don't know why the result of same code gives sometimes correct or sometimes wrong values?
In this code, I have made my number of row/column constant. But if it is unknown then how do I do? So, I have written another code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i,j,line=0,target=0;
int f=getc(myfile);
clrscr();
myfile=fopen("E:\\input.txt", "r");
while (f!= EOF)
{ if(f=='\n')
line++;
f=getc(myfile);
}
//printf("%d",line);
target=++line;
//printf("%d",target);
for(i = 0; i<target; i++)
{
for (j = 0 ; j<target; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
But it is not at all working, everytime is giving wrong values.
Kindly help
It's done with the following code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i=0,j=0,target=-1;
clrscr();
myfile=fopen("E:\\input.txt", "r");
while(1)
{
while(1)
{ fscanf(myfile,"%f %c",&a,&ch);
data[i][j]=a;
j++;
if(ch=='0')
{ target=j;
break;
}
else if(j==target)
break;
}
j=0;
i++;
if(i==target)
break;
}
fclose(myfile);
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
removed the EOF, rather using while loop.