top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Facing an issue while writing program for duplicate occurrence of a character in a string, can any one help?

+5 votes
409 views
#include<stdio.h>
#include<string.h>
main()
{
    char p[100];
    int i,len;
    printf("Enter a string\n");
    scanf("%[^\n]",p);
    len=strlen(p);
    for(i=0;i<len-1;i++)
    {
        if(a[i]==a[i+1])
              p[i]=p[i+1];
    }
    printf("String :  %s\n",p);
}
posted Jul 9, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Dont know what u want to achieve with this program, would you like to explain?

1 Answer

0 votes
#include<stdio.h> 
main()
 { 
     char p[100]; 
     int i,j;
     printf("Enter a string\n"); 
     scanf("%[^\n]",p); 
     for(i=0;p[i];i++) 
     {
          if(a[i]==a[i+1])
          {
                 for(j=i;p[j];j++)
                        p[j]=p[j+1];
                 i--;
           }
      }
      printf("String : %s\n",p); 
}
answer Jul 9, 2015 by Chirag Gangdev
Similar Questions
+3 votes
#include<stdio.h>
#include<string.h>

int main()
{
    char ptr[]= {'a','b','c','0','e'};
    char str[]= "abc0e";
    printf("\nptr = %s\t len = %d\n",ptr, strlen(ptr));
    printf("\nstr = %s\t len = %d\n",str, strlen(str));
    return 0;
}

Output : ptr = abc0e len = 6
str = abc0e len = 5

Why the length for ptr is 6 ? Can someone please explain it ?

0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

...