2 Replies - 1749 Views - Last Post: 04 November 2009 - 10:45 PM Rate Topic: -----

#1 ge0rge007  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 05-June 09

Separate each character of a string at C

Posted 04 November 2009 - 10:16 PM

Hello!!!
I am trying to separate each character of a string with this code but i am getting a "Segmentation fault".
It would be great if could someone tell me what i am doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
		int i;
	  char input[80];
	  printf("Give an arithmetic expression in Inverse Polish Notation: \n");
	  scanf("%s", input);
	  for (i=0; i<strlen(input); i++)
	printf("%s",input[i]);
	return 0;
}


I am running the code at a linux machine thats why there is no system("pause"); at the bottom.
Thanks for your time :)

Is This A Good Question/Topic? 0
  • +

Replies To: Separate each character of a string at C

#2 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Separate each character of a string at C

Posted 04 November 2009 - 10:32 PM

Hello,

View Postge0rge007, on 4 Nov, 2009 - 09:16 PM, said:

Hello!!!
I am trying to separate each character of a string with this code but i am getting a "Segmentation fault".
It would be great if could someone tell me what i am doing wrong.
I am running the code at a linux machine thats why there is no system("pause"); at the bottom.

Thanks for your time :)


Two suggestions
  • dont' do the strlen in the for loop
  • use %c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
	int i;
	char input[80]={0};
	int len=0;
	printf("Give an arithmetic expression in Inverse Polish Notation: \n");
	scanf("%s", input);
	len = strlen(input);
	for (i=0; i<len; i++)
		printf("%c",input[i]);
	return 0;
}



Jw
Was This Post Helpful? 1
  • +
  • -

#3 ge0rge007  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 05-June 09

Re: Separate each character of a string at C

Posted 04 November 2009 - 10:45 PM

Thanks for your help!
I have corrected the code and now it works!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1